• leetcode-946:验证栈序列


    1. bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
    2. int* arr = (int*)malloc(sizeof(int) * pushedSize);
    3. for(int i = 0; i < poppedSize; i++) arr[i] = 1;
    4. int ind = 0;
    5. for(int i = 0; i < poppedSize; i++){
    6. while(arr[ind] == 0 || popped[i] != pushed[ind]) {
    7. ind++;
    8. if(ind == poppedSize) return false;
    9. }
    10. arr[ind] = 0;
    11. while(ind > 0 && arr[ind] == 0) ind--;
    12. }
    13. return true;
    14. }
    1. bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
    2. int* sta = (int*)malloc(sizeof(int) * poppedSize);
    3. int top = -1, x = 0;
    4. for(int i = 0; i < poppedSize; i++){
    5. if(top == -1 || sta[top] != popped[i]){
    6. while(x < poppedSize && pushed[x] != popped[i]){
    7. sta[++top] = pushed[x++];
    8. }
    9. if(x == poppedSize) return false;
    10. sta[++top] = pushed[x++];
    11. }
    12. top--;
    13. }
    14. return true;
    15. }
    1. bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
    2. int* sta = (int*)malloc(sizeof(int) * poppedSize);
    3. int top = -1, x = 0;
    4. for (int i = 0; i < poppedSize; i++){
    5. while(top == -1 || sta[top] != popped[i]){
    6. if(x == poppedSize) return false;
    7. sta[++top] = pushed[x++];
    8. }
    9. top--;
    10. }
    11. return true;
    12. }
    1. class Solution {
    2. public:
    3. bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    4. int n = pushed.size(), x = 0;//x代表入栈序列的下标
    5. stack<int> s;
    6. for(int i = 0; i < n; i++) {
    7. if (s.empty() || s.top() != popped[i]) {
    8. while (x < pushed.size() && pushed[x] != popped[i]) {
    9. s.push(pushed[x]);
    10. x += 1;
    11. }
    12. if (x == pushed.size()) return false;
    13. s.push(pushed[x]);
    14. x += 1;
    15. }
    16. s.pop();
    17. }
    18. return true;
    19. }
    20. };

  • 相关阅读:
    【Java网络原理】 四
    并发修改异常
    市政管理学试题及答案
    密码保护工具的编写
    NC20583 [SDOI2016]齿轮
    【毕业设计】深度学习卫星遥感图像检测与识别系统(目标检测)
    VGGNet架构解析
    TC8:UDP_INTRODUCTION_01-03
    机械转码日记【14】C++运算符重载的应用——实现一个日期类计算器
    每次遇到sass 总要出问题,yarn不行,用cnpm i 就顺利了
  • 原文地址:https://blog.csdn.net/Mz_yuner/article/details/133247124