• 栈的压入,弹出序列


    面试题 22:栈的压入弹出序列,假设压入栈的所有数字都不相等,第一个序列表示压入顺序,第二个为弹出顺序,判断两个顺序是否相对应。

    下面我根据海涛大神的思路自己写代码实现功能

    关键思路指点:当前要弹出的数字不在栈中,就把当前压入序列的数字至要弹出的数字压入栈中,然后弹出要弹出的数字,最后如果栈为空,就说明序列对应,否则不对应

    #include<iostream>
    #include<stack>
    using namespace std;
    
    bool testStack(int *pushSequece, int pushLength, int *popSequece, int popLength){
        //讨论异常情况,如两个长度不相同,或传入空指针
        int pushBegin,popBegin;
        stack<int> mystack;
        pushBegin = popBegin = 0;
        int i;
        for (; popBegin < popLength; popBegin++){
            if (!mystack.size()){
                //stack 为空
                for (i = pushBegin; i < pushLength; i++){
                    if (pushSequece[i] == popSequece[popBegin])
                        break;
                }
                if (i == pushLength) return false;
                for (int j = pushBegin; j <= i; j++){
                    mystack.push(pushSequece[j]);
                }
                pushBegin = i + 1;
                mystack.pop();
            }else {
                if (mystack.top() != popSequece[popBegin]){
                    //如果top元素不为当前遍历popSequece的元素
                    for (i = pushBegin; i < pushLength; i++){
                        if (pushSequece[i] == popSequece[popBegin])
                            break;
                        
                    }
                    if (i == pushLength) return false;
                    for (int j = pushBegin; j <= i; j++){
                        mystack.push(pushSequece[j]);
                        cout << "测试2" << endl;
                        
                    }
                    pushBegin = i + 1;
                    mystack.pop();
                }else{
                    mystack.pop();
                }
                
            }
            
        }
        if (mystack.size() == 0)
            return true;
    
    
    }
    int main(){
        
        int push[] = { 1, 2, 3, 4, 5 };
        int length = sizeof(push) / sizeof(int);
        int pop1[] = { 4, 5, 3, 2, 1 };
        int pop2[] = { 4, 3, 5, 1, 2 };
        int pop3[] = { 1, 2, 3, 4, 5 };
        int pop4[] = { 3, 2, 5, 4, 1 };
        int pop5[] = { 3, 5, 2, 1, 4 };
        if (testStack(push, length, pop1, length))
            cout << "符合条件"<<endl;
        if (!testStack(push, length, pop2, length))
            cout << "不符合条件"<<endl;
        if (testStack(push, length, pop4, length))
            cout << "符合条件" << endl;
        if (!testStack(push, length, pop5, length))
            cout << "不符合条件" << endl;
    
        system("pause");
    }

    关键思路指点:当前要弹出的数字不在栈中,就把当前压入序列的数字至要弹出的数字压入栈中,然后弹出要弹出的数字,最后如果栈为空,就说明序列对应,否则不对应

  • 相关阅读:
    window 7 安装Jmeter并配置https录制脚本
    windows 7安装Fiddler抓HTTPS请求的解决办法
    APP界面设计与页面布局的23条基本原则
    Charles 连接手机抓包出现Unknown,一直无法抓包的问题解决
    Charles 如何破解与连接手机进行抓包
    WeTest----如何查看Wetest生成测试报告?
    WeTest----如何使用WeTest进行App性能测试?
    兼容位图
    DeleteDC ReleaseDC DeleteObject之间的区别
    屏幕保存为位图
  • 原文地址:https://www.cnblogs.com/hzmbbbb/p/3980579.html
Copyright © 2020-2023  润新知