def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
stack,i = [],0
for num in putIn:
stack.append(num)
while stack and stack[-1] takeOut[i]:
stack.pop()
i = i+1
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack stack = new Stack();
int index = 0;
for(int i = 0;i<putIn.length;i++){
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack s = new Stack<>();
int i = 0;
for(int put : putIn){
s.push(put);
while(!s.empty() && s.peek()takeOut[i]){
s.pop();
i++;
}
}
class Solution:
def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
stack, i = [],0
for num in putIn:
stack.append(num)
while stack and takeOut[i] stack[-1]:
stack.pop()
i += 1
return not stack
时间 o(N) 空间O(N)
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
int index1 = 0;
int index2 = 0;
int m = putIn.length;
int n = takeOut.length;
Deque stack = new ArrayDeque<>();
共以下 42 个回答
时间复杂度O(n),空间复杂度O(n)
//栈的压入和弹出& putIn, vector & takeOut) { stk;
//看到之后都读不太懂题目,还是练习的太少了
//还是借用了辅助栈,入栈每一个元素的同时循环判断取出序列组是否有相同的元素
class Solution {
public:
bool validateBookSequences(vector
stack
int i = 0;
for(int num:putIn)
{
stk.push(num);
while(!stk.empty() && stk.top() takeOut[i])
{//循环判断出栈
stk.pop();
i++;
};
”’java& putIn, vector & takeOut) { stk;
class Solution {
public:
bool validateBookSequences(vector
stack
int i = 0;
for(int num : putIn){
stk.push(num);
while(!stk.empty() && stk.top()takeOut[i]){
stk.pop();
i++;
}
}
return stk.empty();
}
};
”’
时间复杂度:o(n)
空间复杂度:o(n)
def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
stack,i = [],0
for num in putIn:
stack.append(num)
while stack and stack[-1] takeOut[i]:
stack.pop()
i = i+1
解题思路:
使用辅助栈 依次压入要Putin的数据
然后确认辅助栈不为空 且每次都检查一下是否跟takeout[i]相同,如果相同则将辅助栈顶元素pop出去,且i=i+1
时间复杂度:o(n)
空间复杂度:o(n)
建立一个辅助栈,以元素出现顺序出栈,看最终的栈空间是否为空& putIn, vector & takeOut) { s1;
时间复杂度o(n)空间复杂度o(n)
class Solution {
public:
bool validateBookSequences(vector
std::stack
int k = 0;
for(int a=0;a<putIn.size();a++)
{
s1.push(putIn[a]);
while(!s1.empty()&&takeOut[k]s1.top())
{
s1.pop();
k++;
}
}
if(s1.empty())
{
return true;
}
else
{
return false;
}
}
};
day5第二题
时间复杂度:O(n方)
空间复杂度:O(1)
栈的压入、弹出序列java
思路:利用栈的先入后出特性,每次将存书序列入栈,同时去判断当前的栈顶是否和当前要取出的书是否相同,相同就取出,即出栈,同时取书序列向后一位,如果最终栈为空,则取书序列是合法的
代码:
class Solution {& putIn, vector & takeOut) { stk;
public:
bool validateBookSequences(vector
stack
if(putIn.size()0) return true;
};
打卡
class Solution { zhan=new Stack<>();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int k=0;
for(int i=0;i<putIn.length;i++)
{
zhan.push(putIn[i]);
while(!zhan.isEmpty()&&zhan.peek().intValue()takeOut[k])
{
zhan.pop();
k++;
}
}
return zhan.isEmpty();
}
}
模拟
java stack = new Stack<>();
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int i = 0;
for(int num : putIn){
stack.push(num);
while(!stack.isEmpty() && stack.peek() takeOut[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
提交
就是栈,判断最后是否栈空,时间复杂度O(n),空间复杂度O(n)
class Solution { stack = new Stack ();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int index = 0;
for(int i = 0;i<putIn.length;i++){
}
时间复杂度O(N),空间复杂度O(N)
class Solution { A = new Stack ();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int i = 0;
for(int num:putIn){
A.push(num);
while(!A.isEmpty() && A.peek() takeOut[i]){
A.pop();
i++;
}
}
return A.isEmpty();
}
}时间复杂度O(n),空间复杂度O(n)
时间复杂度:O(n)
空间复杂度: O(n)
栈结构,先后后出
class Solution {& putIn, vector & takeOut) { stk;
public:
bool validateBookSequences(vector
stack
int i = 0;
for(int num : putIn)
{
stk.push(num);
while(!stk.empty() && stk.top() takeOut[i])
{
stk.pop();
i++;
}
}
return stk.empty();
}
};
打卡
时空复杂度O(N)
class Solution { s = new Stack<>();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int i = 0;
for(int put : putIn){
s.push(put);
while(!s.empty() && s.peek()takeOut[i]){
s.pop();
i++;
}
}
}
//时间复杂度On
//空间复杂度On
时间复杂度O(n),空间复杂度O(n)
时间复杂度:O(n)
空间复杂度:O(n)
打卡
https://leetcode.cn/problems/zhan-de-ya-ru-dan-chu-xu-lie-lcof/solutions/2670683/yan-zheng-tu-shu-qu-chu-shun-xu-by-yukis-vhzv
public boolean validateBookSequences(int[] putIn, int[] takeOut) { A= new Stack<>();
Stack
int j=0;
if(putIn.length0||putIn.length1) return true;
for(int num:putIn){
A.push(num);
while(!A.isEmpty() && A.peek()takeOut[j]){
A.pop();
j++;
}
}
return A.isEmpty();
思路:每个字符都压栈,遇到和输出字符一样的再重复出栈,最后看栈是否为空
核心点在于理解压入弹出序列的知识(我之前学过关于这个知识点,所以理解起来不难),还有要创建一个辅助栈 stack = new Stack(); popped[k]){
public boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed null || pushed.length <= 0){
return true;
}
int k = 0;
Stack
for(int i = 0; i < pushed.length; i++){
stack.push(pushed[i]);
while(!stack.isEmpty() && stack.peek()
stack.pop();
k++;
}
}
return stack.isEmpty();
}
class Solution {& putIn, vector & takeOut) { st;
public:
bool validateBookSequences(vector
stack
int i = 0;
for (auto num : putIn){
st.push(num);
while (!st.empty() && st.top() takeOut[i]){
st.pop();
++i;
}
}
return st.empty();
}
};输入和输出用栈来维护,时空复杂度都是O(N)
class Solution:
def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
stack, i = [],0
for num in putIn:
stack.append(num)
while stack and takeOut[i] stack[-1]:
stack.pop()
i += 1
return not stack
时间 o(N) 空间O(N)
class Solution {& putIn, vector & takeOut) { s1;
public:
bool validateBookSequences(vector
stack
int j = 0;
for(int i = 0; i < putIn.size(); i ++){
s1.push(putIn[i]);
while(!s1.empty() && s1.top() takeOut[j]){
s1.pop();
j ++;
}
}
return s1.empty();
}
};
时间复杂度 空间复杂度均为O(n)
时间复杂度O(n)
空间复杂度O(n)
“`C++& putIn, vector & takeOut) {
class Solution {
public:
bool validateBookSequences(vector
if(putIn.empty() == true)
{
return true;
}
};
“`
class Solution { stack = new ArrayDeque<>();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
int index1 = 0;
int index2 = 0;
int m = putIn.length;
int n = takeOut.length;
Deque
}
时间复杂度O(n),空间复杂度O(n)。
时间复杂度$O(n)$,额外空间复杂度$O(n)$
按照顺序进栈,然后比较出的顺序,如果出的值与栈顶元素相等就出栈,时间复杂度:O(n),空间复杂度:O(n)
我们按照顺序入栈和出栈,如果最后栈空,则返回true;否则反馈false;
时间复杂度:O(n)
额外空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(n)