class Solution:
def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
stack=[]
for i in range(len(putIn)):
if not stack or takeOut[0]!=stack[-1]:
stack.append(putIn[i])
while takeOut and stack and takeOut[0]==stack[-1]: # 当两者都有数据时,才可以比较。而且一直比较模拟栈栈顶元素是否可以在当下被取出(while),takeOut顺序中值不重复。如何判断这就是跳出这个while,当已经遍历完putIn后,就是最后的状态:极端状态,如果模拟栈一直没有pop,一直添加完putIn,那么就会while一直和takeOut顺序元素比较pop stack[-1]栈顶元素,如果stack为空了,那么就说明都pop完了,顺序是对的,此时takeOut也遍历完了。
stack.pop()
takeOut.pop(0)
if stack: # 如果模拟栈中还有数据,那么不对,说明顺序有误,不然就已经pop清空了。takeOut也不为空
return False
else:
return True
class Solution:
def validateBookSequences(self, putIn: List[int], takeOut: List[int]) -> bool:
simu_stack = []
pop_count = 0
for num in putIn:
simu_stack.append(num)
while simu_stack and pop_count < len(takeOut) and simu_stack[-1] == takeOut[pop_count]:
# try to pop all that equals
simu_stack.pop()
pop_count += 1
return not len(simu_stack)
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
int i =0;
int j = 0;
int m = putIn.length;
Stack<Integer> s = new Stack<>();
int cnt = 0;
while(i<m){
if(i<m){
s.push(putIn[i]);
i++;
}
while(!s.empty()&&s.peek()==takeOut[j]){
s.pop();
j++;
}
}
return s.empty();
}
}
共以下 27 个回答
###
【不建议】题解:按取出的数组先遍历,只要取出的值在栈中不存在,就去入栈数据遍历进行放入。
【建议】改进:按照putIn进行遍历数据加入栈,如果栈顶数据能够被take出来,最后判断栈是否为空即可。
好处是,遍历putIn可以把元素放到栈里面暂存,代码能够写的更加讲解一些
class Solution {& putIn, vector & takeOut) { m_sk;//辅助栈
public:
bool validateBookSequences(vector
std::stack
auto i=0,j=0;
while(i<takeOut.size())
{
if(m_sk.empty()||m_sk.top()!=takeOut[i]&&j<putIn.size())
{
while(j<putIn.size())
{
if(putIn[j]!=takeOut[i])
{
m_sk.push(putIn[j++]);
};
//使用辅助栈
思路 用一个stack模拟 然后用贪心的想法 把能按顺序pop的都pop了 如果能pop完就说明可以
space O(n) time O(n)
以前选择题经常这么考
思路:
(1)将takeout中的元素依次作为target目标值;
(2)将putin元素按顺序压入栈中,直至栈顶元素和target相等(这时候才能取出目标值):目标值后移+删除栈顶元素(循环至栈顶元素!=目标值)。
如果最后栈空,说明能够按照takeout的顺序读取元素。
时间:o(n)
空间:o(n)
思路:设一个栈模拟,按入栈数组顺序依次入栈,每次加入新元素后都与当前指向出栈数组的元素判断,相同则弹出栈,直到不同为止再新加入元素。最后,如果栈内没有元素存在,则意味着顺序是正确的,反之则序列不成立
时间O(N),空间O(N)
时间复杂度O(n)
空间复杂度O(n)
思路是对的,但是少了stack为empty的边界值判断
时间复杂度:O(n)
空间复杂度:O(n)
思路: 根据两个序列进行出入栈操作,若最后栈空 说明有效
模拟栈操作
“`c++& putIn, vector & takeOut) { sk;=len&&sk.size())return false;
bool validateBookSequences(vector
stack
int len=putIn.size();
int inid=0,outid=0;
while(outid
}
return true;
}
“`
用一个栈模拟存取操作,模拟成功就返回true 失败就返回false
class Solution { st = new Stack();
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
Stack
int j = 0;
for(int x : putIn){
st.push(x);
while(j < takeOut.length && !st.isEmpty() && st.peek() takeOut[j]){
st.pop();
j++;
}
}
return j takeOut.length;
}
}
时间:O(n)
空间:O(n)
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
int p1 = 0; //putin栈指针
int index = 0; // 用于指向 takeOut 数组中的元素
}
思路:画图辅助确实可以帮助理解更快并且思路也更清晰,返回检查的时候也能快速推导。时间空间复杂度都是O(N),因为要遍历,还有辅助栈存储
时间复杂度O(n),空间复杂度O(n)
打卡
时间复杂度O(n)空间复杂度O(n)
时间复杂度:O(n) 遍历了一遍数组
额外空间复杂度:O(n)
模拟 stk = new Stack<>(); takeOut[j]){
class Solution {
public boolean validateBookSequences(int[] putIn, int[] takeOut) {
if(putIn.length 0) return true;
Stack
int i = 0, j = 0;
while(i < putIn.length){
stk.push(putIn[i++]);
while(!stk.isEmpty() && stk.peek()
j++;
stk.pop();
}
if(j takeOut.length) return true;
}
return false;
}
}
···cpp& putIn, vector & takeOut) { s;
class Solution {
public:
bool validateBookSequences(vector
if(putIn.empty()) return true;
stack
int cnt_o = 0;
for(int i = 0;i < putIn.size();i ++){
s.push(putIn[i]);
while(!s.empty() && s.top() takeOut[cnt_o]){
s.pop();
cnt_o++;
}
}
return s.empty();
}
};
// 思路:利用栈先入后出的性质
// 时间复杂度 o(n) 空间复杂度 o(n)
“`
思路:通过栈来模拟书架的操作,确保每次出栈顺序与
takeOut数组一致。时间复杂度:O(n),遍历了
putIn数组。空间复杂度:O(n),使用了栈,元素个数为
n。