标签:return int 31 next state OK stack leetcode pushedSize
// language c
// 剑指31
// https://leetcode-cn.com/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
if(pushedSize==0)
return true;
// 先实现个栈吧
int stack[pushedSize];
int next= 0; // 当前栈内元素数量
int p =0; // push序列进了几个了
int done = 0; //完成了几个了
void push(){
stack[next++] = pushed[p++];
}
int pop(){
done++;
return stack[--next];
}
int gettop(){
return stack[next-1];
}
bool empty(){
if(next==0)
return true;
return false;
}
// 感觉关键是要回溯,要个锤子回溯,扯卵蛋
// int state[pushedSize];
// int next_state;
// int p_state;
// void beifen(){
// for(int i =0; i<next; i++){
// state[i] = stack[i];
// }
// next_state = next;
// p_state = p;
// }
// void huifu(){ // 恢复
// for(int i =0; i<next; i++){
// stack[i] = state[i];
// }
// next = next_state;
// p = p_state;
// }
// 4 5 3 2 1
while(done<pushedSize){// 每一轮循环 check pop序列中的下一个是否能找到,不能的话就false
// beifen();
int target = popped[done];
if(empty())
push();
while((gettop()!=target) ){ // 停止条件,顶就是所要,或者进完了
if(p==pushedSize)
break;
push();
}
if(pop() !=target){
return false;
}
}
return true;
}
标签:return,int,31,next,state,OK,stack,leetcode,pushedSize 来源: https://www.cnblogs.com/gallien/p/14337836.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。