zoukankan      html  css  js  c++  java
  • 剑指offer22 栈的压入、弹出序列

    写的一个代码,虽然正确通过了,但我觉得会报vector越界的错误

    class Solution {
    public:
        bool IsPopOrder(vector<int> pushV,vector<int> popV) {
            int length1 = pushV.size();
            int length2 = popV.size();
            if(length1 <= 0 || length2 <= 0 || length1 != length2)
                return false;
            stack<int> sta;
            int current = 0;
            for(int i = 0;i < length2;i++){
                if(sta.empty()){
                    while(pushV[current] != popV[i]){
                        if(current >= length1)
                            return false;
                        sta.push(pushV[current]);
                        current++;
                    }
                    current++;
                }
                else{
                    if(sta.top() == popV[i]){
                        sta.pop();
                    }
                    else{
                        while(pushV[current] != popV[i]){
                            if(current >= length1)
                                return false;
                            sta.push(pushV[current]);
                            current++;
                        }
                        current++;
                    }
                }
            }
            return true;
        }
    };
  • 相关阅读:
    Python字符串
    ListCtrl控件
    leetcode1004
    leetcode1003
    leetcode1002
    leetcode153
    leetcode540
    leetcode435
    leetcode999
    leetcode997
  • 原文地址:https://www.cnblogs.com/ymjyqsx/p/7238608.html
Copyright © 2011-2022 走看看