/************
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;
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。