牛客--栈的压入弹出序列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ferlan/article/details/86490312

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能

为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈

的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不

可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

思路:

我们直接用例题看,出栈序列为{4,5,3,2,1}。

4: 1,2,3,4入栈,4出栈

5: 5入栈,5出栈

3: 3出栈

2: 2出栈

1: 1出栈

我们发现出栈序列的每个值(比如a),都要先检查包括a在内且a之前的值是否

都已经在栈中,没有则统统先入栈,然后在出栈a,且我们要出栈的值最后一定

在栈顶。那么就好判断了,只要确保我们的值在栈中,直接判断栈顶是否和a值

相同,不同就说明出栈序列错误。直到出栈序列走完,栈也为空时,说明出栈序

列是正确的。

代码实现
#include<iostream>
#include<vector>
#include<stack>

//思路:
//我们发现出栈序列的每个值(比如a),都要先检查包括a在内且a之前的值是否
//
//都已经在栈中,没有则统统先入栈,然后在出栈a,且我们要出栈的值最后一定
//
//在栈顶。那么就好判断了,只要确保我们的值在栈中,直接判断栈顶是否和a值
//
//相同,不同就说明出栈序列错误。直到出栈序列走完,栈也为空时,说明出栈序
//
//列是正确的。
using namespace std;
class Solution {
public:
	bool IsPopOrder(vector<int> pushV, vector<int> popV) {
		if (pushV.empty() && popV.empty())
			return true;
		stack<int> s;
		vector<int>::iterator p1 = pushV.begin();
		vector<int>::iterator p2 = popV.begin();


		while (p2 != popV.end() || !s.empty())
		{
			vector<int>::iterator it = find(pushV.begin(), pushV.end(), *p2);
			if (p1 > it)
			{
				if (s.top() != *p2)
				{
					return false;
				}

			}
			else//说明再该数之前还有数要入
			{

				while (p1 <= it)
				{

					s.push(*p1);
					p1++;

				}

			}
			s.pop();
			p2++;
		}
		return true;

	}

};


int main()
{
	int a[] = { 1,2,3,4,5 };
	int b[] = { 4,5,3,2,1 };
	vector<int> v1(a, a + sizeof(a) / sizeof(a[0]));
	vector<int> v2(b, b + sizeof(b) / sizeof(b[0]));
	Solution s;
	s.IsPopOrder(v1, v2);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ferlan/article/details/86490312