本文主要是介绍*剑指offer(第二版)31 栈的压入、弹出序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&tqId=11174&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
开一个辅助栈写代码会更清晰,虽然感觉不开写细致也能A掉
class Solution {
public:bool IsPopOrder(vector<int> pushV,vector<int> popV) {if (pushV.size() != popV.size()) return false;if (pushV.size() == 0 && popV.size() == 0) return true;stack<int> in;int pop_ptr = 0;for (int i = 0; i < pushV.size(); i++) {if (pushV[i] == popV[pop_ptr]){in.push(pushV[i]);while ( !in.empty() && pop_ptr < popV.size() && in.top() == popV[pop_ptr]) {in.pop();pop_ptr ++;}} else {in.push( pushV[i] );}}return pop_ptr >= popV.size() && in.empty();}
};
这篇关于*剑指offer(第二版)31 栈的压入、弹出序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!