class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack<Integer> stack = new Stack<>();
int i=0;
for (int num : putIn) {
stack.push(num);
while (!stack.isEmpty()&& stack.peek()==takeOut[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
stack = []
j = 0
for i in pushed:
stack.append(i)
while stack and stack[-1] == popped[j]:
stack.pop()
j += 1
return not stack
共以下 26 个回答
bool validateStackSequences(int pushed[],int poped[],int n)
{
int stack[5];
int top = -1;
int pushIndex = 0;
}
void printStackSquence(int pushed[],int poped[],int n)//打印栈,弹出序列
{
print(“in stack: “);
for(int i = 0;i < n;i++)
{
printf(“%d “,pushed[i]);
}
printf(“\n”);
printf(” out stack “);
for(int i = 0;i < n;i++)
{
printf(“%d “,poped[i]);
}
printf(“\n”);
}//时间复杂度O(n),空间复杂度O(n);
class Solution {& pushed, vector & popped) {
public:
bool validateStackSequences(vector
};
时间复杂度:O(n^2),空间复杂度:O(n)
打卡
思路
解题方法
复杂度
时间复杂度:
空间复杂度:
Code
class Solution {& pushed, vector & popped) { st;
public:
bool validateStackSequences(vector
stack
int n = poped.size();
for(int i=0, j=0; i<n; i++){
st.emplace(pushed[i]);
while(!st.empty()&&st.top() popped[j]){
st.pop();
j++;
}
}
return st.empty();
}
};
时间复杂度:o(n)
空间复杂度:o(n)
事件复杂度o(n),空间复杂度o(n);& pushed, vector & popped) { s;
class Solution {
public:
bool validateStackSequences(vector
stack
int n = pushed.size();
for (int i = 0, j = 0; i < n; ++i) {
s.emplace(pushed[i]);
while (!s.empty() && s.top() popped[j]) {
s.pop();
j++;
}
}
return s.empty();
}
};
//时间复杂度为o(n)
//空间复杂度为o(n)
class Solution {& pushed, vector & popped) { ans_stack;
public:
bool validateStackSequences(vector
stack
int count=pushed.size();
};
时间复杂度:O(n)& pushed, vector & popped) { stack1;
空间复杂度:O(n)
class Solution {
public:
bool validateStackSequences(vector
stack
for(int i=0,j=0;i<pushed.size();i++){
stack1.push(pushed[i]);
while((!stack1.empty())&&stack1.top()popped[j]){
stack1.pop();
j++;
}
}
return stack1.empty();
}
};
//先反转再比值,for top cong0——》1,while里再-1;就变回了0;加一减一相等的话,证明;
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
int stack= (int )malloc(sizeof(int )*pushedSize);
int top=0;
for(int i=0,j=0;i<pushedSize;i++){
stack[top++]=pushed[i];//先复制push到stack里/首先是比s1和pop0;成功则j+1,到poped1;
while(top>0&&stack[top-1]popped[j]){
top–;
j++;
}
}
free(stack);
return top0;
}
学习了
时间复杂度O(n),空间复杂度O(n)
模拟就好了,O(N)
代码写的复杂了,记录下,顺带贴上修改之后的 stack = new Stack ();
”’java
//判断栈的出栈序列是否合法
Stack
修改之后的 stack = new Stack (); poped[j]) {
//上述代码逻辑重复了,简化一下
if(pushed null || pushed.length <= 0) {
return true;
}
int j = 0;
Stack
for(int m=0; m<pushed.size(); m++) {
stack.push(pushed[m]);
while(!stack.isEmpty() && stack.peek()
stack.pop();
j++;
}
}
return stack.empty();
“`python
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
#pushed没有记录入栈的顺序,但是popped记录了出栈的顺序,对于每一个入栈的元素,通过跟pushed序列比较,可以知道它在对应的位置是否有出栈,如果这个位置没有出栈,那说明下一个动作是下一个元素入栈。因此用一个辅助栈记录入栈动作,每次按照pushed的顺序入栈一个元素,这个元素跟popped的出栈顺序比较,未出栈则继续入栈,继续找栈顶元素的出栈位置,找到后并出栈。
stack = []
i = 0
“`python
压入栈是判断栈顶元素是否是弹出的元素,如果是就一直弹出,直到栈顶元素不再是弹出元素序列中的值,最后压栈完毕,判断栈是否全部弹出,如果是说明符合。
这个有意思
以前学数据结构常考的点
時間複雜度 : O(n) 空間複雜度: O(n)
利用一個輔助的stack來壓入pushed中的元素, 並在壓入元素後, 透過stack頂部元素與popped中的元素比較
如果相同則彈出stack頂部元素, 否則就繼續壓入剩餘元素, 最後如果是合法的popped 則stack應該為空.
使用一个辅助栈
直接用一个栈压入pushed数组,然后逐个比较popped数组元素,最后看栈是否为空
“`cpp st; & pushed, vector & popped) {
class Solution {
public:
stack
bool validateStackSequences(vector
int i = 0;
int j = 0;
int in = pushed.size();
int jn = popped.size();
while(i < in){ st.push(pushed[i++]); while(j < jn && !st.empty() && st.top() == popped[j]){ st.pop(); j++; } } return st.empty(); } };
1、辅助栈。
时间O(n),空间O(n)