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

    输入两个整数序列,第一个序列表示的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列45,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

    #include "stdafx.h"
    #include <string>
    using namespace std;
    #include<stack>
    #include <vector>



    实现  栈的压入、弹出序列
    class Solution
    {
        public:

            bool IsPopOrder(vector<int> pushA, vector<int> popA)
            {
                if (pushA.size() == 0 || popA.size() == 0)
                {
                    return false;
                }
                for (int i = 0, j = 0; i < pushA.size(); i++)
                {
                    st.push(pushA[i]);
                    if (j < popA.size() && st.top() == popA[j])
                    {
                        st.pop();
                    }

                }
                return st.empty();
            }

            Solution();
            ~Solution();

        private:
            stack <int> st;
    };

    Solution::Solution()
    {
    }

    Solution::~Solution()
    {
    }



    int main()
    {
        Solution sou;
        vector<int> aa = { 1, 2, 3, 4, 5 };
        vector<int> bb = { 4, 3, 5, 1, 2 };
        bool bRet = sou.IsPopOrder(aa, bb);
        return 1;
    }

    天天向上
  • 相关阅读:
    python路径相关
    python之json
    python之正则表达式备忘
    MD5 SHA1 HMAC HMAC_SHA1区别
    微信根据openid给用户发送图文消息
    最近做的几个小程序
    5000万pv小程序,高并发及缓存优化,入坑
    小程序 后台发送模板消息
    mysql 组合索引
    php 拆分txt小说章节保存到数据库
  • 原文地址:https://www.cnblogs.com/hg07/p/12722956.html
Copyright © 2011-2022 走看看