温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

小代码 栈之合法性一点思路 不一样的际遇

发布时间:2020-07-08 20:35:35 来源:网络 阅读:197 作者:wzdouban 栏目:编程语言
/************
1 2 3 4 如栈
出栈 
3 2 4 1 返回0
错误 3 1 2 4 返回-1
**********/
#include<iostream>
#include<stack>
#include<string.h>

using namespace std;

bool judge(const char *push_seq, const char *pop_seq)
{
    if (NULL == push_seq || NULL == pop_seq)
    {
        return false;         
    }
    if (strlen(push_seq) != strlen(pop_seq))
    {
        return false;
    }
   
    stack<char>sc;
    while (*push_seq)
    {
        if (0 == sc.size() || sc.top() != *pop_seq)
        { 
            sc.push(*push_seq++); //
        }
        else
        { 
            sc.pop();
            ++pop_seq; //
        }
    }
    while (sc.size())
    {
        if (sc.top() != *pop_seq++)
        {
            return false;
        }
        sc.pop();
    }
    
    return true;
}  
int mycheck1(int a[],int n)
{
    stack<int>s;
    int i,j,k=1;          // 3 2 4 1
    for(i=0;i<n;i++)
    {
        if(a[i]>k)
          { for(j=k;j<a[i];j++) s.push(j);
                k=j+1;cout<<k<<endl;
              continue;
             }
       
        if(a[i]==s.top()){s.pop();continue;}
        if(a[i]<s.top()) return -1;
    }
    cout<<endl;
    return 0;
}

int mycheck(int a[],int n)
{
    stack<int>s;      s.push(0);
    int i,j,k=1;         //BUG段  经过分析 在于s.top段  因为没有元素时这样做有越界的含义
    for(i=0;i<n;i++)
    {
        if(a[i]>k)
          { for(j=k;j<a[i];j++) s.push(j);
                k=j+1;cout<<k<<endl;
              continue;
             }
       
        if(a[i]==s.top()){s.pop();continue;}
        if(a[i]<s.top()) return -1;
    }
    cout<<endl;
    return 0;
}
int main()
{
     //cout<<judge("1234","3241")<<endl;
     //int a[]={1,2,3,4};  
int a[]={3,2,4,1};
// int a[]={4,3,2,1};ok
      cout<<mycheck(a,4);

     
    return 0;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI