题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

#include <stdbool.h>
bool IsPopOrder(int* pushV, int pushVLen, int* popV, int popVLen ) {
    int arr[1000];
    int i = 0;//辅助数列的下标
    int po = 0;//弹出数列的下标
    int pu = 0;//压入数列的下标
  
  	//遍历压入数列,把压入数列的数字放进辅助数列中
    for (; pu < pushVLen; pu++) {
        arr[i] = pushV[pu];
        i++;
	  	//这里做判断,每次压入的数字如果等于弹出数列下标指向的数字,
	 	//就把辅助数列的下标向后移动一格,弹出数列的下标向后移动一格,
        while (arr[i - 1] == popV[po]) {
		  //辅助数列i=0时为空,结束
            if(i==0){
                break;
            }
            i--;
            po++;
        }
    }
  //如果出栈的顺序符合逻辑,那么辅助队列的数字可以全部弹出,此时i=0
    if (i == 0)
        return true;
    else
        return false;

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务