标签:sys == 情况下 private nbsp ++ main 判断 int
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
#include<stdio.h>
#include<string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
using namespace std;
class Solution {
public:
bool IsPopOrder(vector<int> pushV, vector<int> popV) {
bool isPossible = false;
int i = 0, j = 0;
if (popV.size() != pushV.size())
return false;
while (j<popV.size())
{
/*将pushV中的每个数和popA进行比较,如果不相等则把pushV[i]压入栈,如果相等同时越过
注:刚开始时s1为空的情况下吧??*/
while (i<pushV.size() && pushV[i] != popV[j])
{
s1.push(pushV[i]);
i++;
}
cout << s1.size()<<" ";
cout << i << " ";
cout << j << endl;
i++;
j++;
/*检查已经入栈的数和下一个出栈数是否相等,如果相等则进行popV下一个数字的比较,同时把s1中相等的数pop出来
不等则继续进行push的压入操作*/
while (!s1.empty() && j<popV.size())
{
int top = s1.top();
if (top == popV[j]){
j++;
s1.pop();
}
else{
//s1.push(popV[j]);
//这里只是跳出了第一次循环
break;
}
}
if (i >= pushV.size() && s1.empty())
isPossible = true;
}
return isPossible;
}
private:
stack<int> s1;
};
int main()
{
Solution s;
vector<int> pushV = { 1, 2, 3, 4, 5 };
vector<int>popV = { 4,5, 3, 2, 1 };
cout << s.IsPopOrder(pushV,popV);
system("pause");
return 0;
}
标签:sys == 情况下 private nbsp ++ main 判断 int
原文地址:http://www.cnblogs.com/bananaa/p/7717770.html