• 栈的压入、弹出序列


    题目:判断一数字序列是否为这些数字入栈的一种出栈方式(前提:栈中的数字不重复)

    思路1:如果下一个弹出的数字刚好是栈顶数字,那么直接弹出。如果下一个弹出的数字不在栈顶,我们把压栈序列还没有入栈的数字压入辅助栈,知道把下一个要弹出的数字压入栈顶为止。如果所有的数字都压入了仍然没有找到下一个弹出的数字,那么该序列不可能是一个弹出序列。

    思路2:开辟一个辅助栈,模拟入栈出战过程(假设pa为入栈序列,pb为出战序列),pA中的元素依次压入辅助栈,新压入的元素与弹出序列的栈底相同,辅助栈弹出,同时pB向上移动,不相同了pB中的元素继续入辅助栈。

     1 #include "stdafx.h"
     2 #include <stack>
     3 
     4 //方法1 
     5 bool IsPopOrder(const int* pPush, const int* pPop, int nLength)
     6 {
     7     bool bPossible = false;
     8     
     9     if(pPush != NULL && pPop != NULL && nLength > 0)
    10     {
    11         const int* pNextPush = pPush;
    12         const int* pNextPop = pPop;
    13         
    14         std::stack<int> stackData;
    15         
    16         while(pNextPop - pPop < nLength)
    17         {
    18             // 当辅助栈的栈顶元素不是要弹出的元素
    19             // 先压入一些数字入栈
    20             while(stackData.empty() || stackData.top() != *pNextPop)
    21             {
    22                 // 如果所有数字都压入辅助栈了,退出循环
    23                 if(pNextPush - pPush == nLength)
    24                     break;
    25                 
    26                 stackData.push(*pNextPush);
    27                 
    28                 pNextPush ++;
    29             }
    30             
    31             if(stackData.top() != *pNextPop)
    32                 break;
    33         
    34             stackData.pop();
    35             pNextPop ++;
    36         }
    37         
    38         if(stackData.empty() && pNextPop - pPop == nLength)
    39             bPossible = true;
    40     }
    41     
    42     return bPossible;
    43     
    44 }
    45 
    46 //方法2 
    47 bool IsPopOrder1(const int* pPush, const int* pPop, int lengthA, int lengthB)
    48 {
    49     if( lengthA != lengthB || lengthA == 0)
    50         return false;
    51     bool flag = false;
    52 
    53     int pA =0;
    54     int pB =0;
    55     int *newpPush = new int[lengthA];
    56     int top = -1;
    57     for(pA = 0 ; pA < lengthA; ++pA)
    58     {
    59         ++top;
    60         newpPush[top] = pPush[pA];
    61         while(newpPush[top] == pPop[pB])
    62         {
    63             --top;
    64             ++pB;
    65         }
    66     }
    67     if(top == -1)
    68         flag = true;
    69     delete []newpPush;
    70     return flag;
    71 }
    72 
    73 int main()
    74 {
    75     const int nLength = 5 ;
    76     int push[nLength] = {1,2,3,4,5};
    77     int pop[nLength] = {4, 5, 3, 2, 1};
    78     
    79     bool  flag1 = IsPopOrder(push, pop, nLength);
    80     printf("Solution 1 is %d
    ", flag1 );
    81     
    82     bool  flag2 = IsPopOrder1(push, pop, nLength, nLength);
    83     printf("Solution 2 is %d", flag2 );
    84     return 0;
    85 }

  • 相关阅读:
    作业1-四则运算题目生成程序
    实验四 决策树算法及应用
    实验三朴素贝叶斯算法及应用
    自定义博客园背景
    机器学习 实验二 K-近邻算法及应用
    机器学习 实验一 感知器及其运用
    实验三 面向对象分析与设计
    实验二 结构化分析与设计
    实验一:软件开发文档与工具的安装与使用
    朴素贝叶斯学习日志——简单案例python计算过程
  • 原文地址:https://www.cnblogs.com/sankexin/p/5619996.html
Copyright © 2020-2023  润新知