【done+重点】剑指offer——面试题22:栈的压入、弹出序列

力扣

// 自己代码略繁琐
class Solution {
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        if (putIn.length == 0) {
            return true;
        }
        Stack<Integer> stack = new Stack<>();
        for (int i = 0, j = 0; i < putIn.length && j < takeOut.length; ++i) {
            stack.push(putIn[i]);
            while (!stack.isEmpty() && stack.peek() == takeOut[j]) {
                stack.pop();
                ++j;
            }
        
        }

        return stack.isEmpty();
    }
}

// 参考答案简练!
class Solution {
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int num : putIn) {
            stack.push(num);
            while (!stack.isEmpty() && stack.peek() == takeOut[j]) {
                stack.pop();
                ++j;
            }
        }

        return stack.isEmpty();
    }
}

// 自己答案太繁琐!!!
class Solution {
    public boolean validateBookSequences(int[] putIn, int[] takeOut) {
        if (putIn.length == 0) {
            return true;
        }
        Stack<Integer> stack = new Stack<>();
        int i = 0, j = 0;
        while (j < takeOut.length) {
            if (stack.isEmpty() && i < putIn.length) {
                stack.push(putIn[i++]);
            } else if (!stack.isEmpty() 
                        && i < putIn.length 
                        && stack.peek() != takeOut[j]) {
                stack.push(putIn[i++]);
            } else if (!stack.isEmpty() && stack.peek() == takeOut[j]) {
                stack.pop();
                ++j;
            } else if (i >= putIn.length) {
                break;
            }
        }

        return stack.isEmpty();
    }
}

##Solution1:
要理解并背诵此题!
参考网址:
https://www.cnblogs.com/AndyJee/p/4651281.html
https://www.nowcoder.com/profile/8709341/codeBookDetail?submissionId=17099336
思路:
1.建立一个辅助栈;
2.将第一个序列的数字压入辅助栈;
3.如果第二个序列的下一个弹出数字刚好是栈顶数字,则直接弹出,第二个序列弹出当前数字,辅助栈也弹出该数字;
4.否则,就把第一个序列中尚未入栈的数字压入辅助栈,直到把第二个序列中下一个需要弹出的数字压入栈顶为止。
5.如果第一个序列的所有数字都已经入栈了,第二个序列中仍然有数字未被弹出,即辅助栈未被清空。则return false。

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if(pushV.size() == 0) return false;
        vector<int> stack;
        for(int i = 0,j = 0 ;i < pushV.size();){
            stack.push_back(pushV[i++]);
            while(j < popV.size() && stack.back() == popV[j]){
                stack.pop_back();
                j++;
            }      
        }
        return stack.empty();
    }
};

##Solution2:
20180901重做

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if (pushV.size() == 0) return false;
        stack<int> push_stack;
        for (int i = 0,j = 0 ;i < pushV.size();) {
            push_stack.push(pushV[i++]);
            while (j < popV.size() && push_stack.top() == popV[j]) {
                push_stack.pop();
                j++;
            }
        }
        return push_stack.empty();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值