输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
1. 0<=pushV.length == popV.length <=1000
2. -1000<=pushV[i]<=1000
3. pushV 的所有数字均不相同
[1,2,3,4,5],[4,5,3,2,1]
true
可以通过push(1)=>push(2)=>push(3)=>push(4)=>pop()=>push(5)=>pop()=>pop()=>pop()=>pop() 这样的顺序得到[4,5,3,2,1]这个序列,返回true
[1,2,3,4,5],[4,3,5,1,2]
false
由于是[1,2,3,4,5]的压入顺序,[4,3,5,1,2]的弹出顺序,要求4,3,5必须在1,2前压入,且1,2不能弹出,但是这样压入的顺序,1又不能在2之前弹出,所以无法形成的,返回false
function IsPopOrder(pushV, popV) {
// write code here
// 还有双指针法可以使用
let steps = pushV.length + popV.length;
let stack = [];
let i = 0;
while (i < steps) {
// 执行步骤,先入栈
if (i < pushV.length) stack.push(pushV[i]);
// 进行是否出栈判断
while (stack.length && stack[stack.length - 1] === popV[0]) {
stack.pop();
popV.shift();
}
i++;
}
return !popV.length;
}
function IsPopOrder(pushV, popV)
{
// write code here
let p1 = 0;
let stack = [];
for(let i=0; i < pushV.length; i++) {
stack.push(pushV[i]);
while (stack && p1 < popV.length && stack[stack.length - 1] === popV[p1]) {
stack.pop();
p1++;
}
}
if (stack.length === 0) return true;
return false;
} function IsPopOrder(pushV, popV)
{
// write code here
if(pushV == null && popV == null) return;
let stack = [];
let index = 0;
for(let i = 0;i < pushV.length; i++){
stack.unshift(pushV[i]);
while(stack.length && stack[0] == popV[index]){
index++;
stack.shift();
}
}
return (stack.length == 0);
} function IsPopOrder(pushV, popV)
{
let x = pushV.indexOf(popV[0]);
//初始化栈数组
let stack = pushV.slice(0, x + 1);
//剩余未入栈数组
let last = pushV.slice(x + 1);
//全部入栈
if(last.length === 0){
for(let k = 0; k < stack.length; k++){
if(stack[k]!==popV[stack.length-k-1]){
return false
}
}
}
popV.shift();
stack.pop();
if(popV.length===0&&last.length!==0){
return false;
}
while(popV.length !== 0){
//当前栈含有出栈第一个
if(stack.indexOf(popV[0])!==-1){
console.log("1:",stack,last,popV)
if(stack[stack.length-1]!==popV[0]){
console.log("f1")
return false
}else{
popV.shift();
stack.pop();
}
}
//当前栈不含有出栈第一个,进行入栈
else{
console.log(stack,last,popV)
//判断需要入栈几个
let i = last.indexOf(popV[0]);
for(let j = 0; j <= i; j++){
stack.push(last[j])
}
if(last[i]!==popV[0]){
console.log(stack,last,popV)
console.log("f2")
return false;
}
else{
last = last.slice(i + 1);
popV.shift();
stack.pop();
}
}
}
return true
} function IsPopOrder(pushV, popV)
{
// write code here
let stack = [];
let j = 0;
let length = pushV.length;
for(let i = 0;i < length;i++) {
stack.push(pushV[i]);
// 如果没有j<length显示条件,stack为空时,stack[-1]是undefined
// popV[j]也是undefined,因为此时j已经超过了数组的长度,就是死循环了
while(j < length && popV[j] === stack[stack.length - 1]) {
stack.pop();
j++;
}
}
return stack.length === 0;
}
module.exports = {
IsPopOrder : IsPopOrder
}; function IsPopOrder(pushV, popV)
{
// write code here
var stack = [];
var j = 0;
for(var i = 0;i<pushV.length;i++){
stack.push(pushV[i]);
while(j < popV.length && (stack[stack.length-1] == popV[j])){
stack.pop();
j++;
}
}
if(stack.length > 0){
return false;
}else{
return true;
}
} function IsPopOrder(pushV, popV)
{
var res=[];
var item=popV.shift();//获取popV中第一个的值
var i,temp;
for(i=0;i<pushV.length;i++){
temp=pushV[i];
if(temp!=item){
res.push(temp)
}
else{
item=popV.shift();
}
}
for(i=0;i<res.length;i++){
temp=res[res.length-1-i];
if(temp!=item)
return false;
item=popV.shift();
}
return true;
} /**
* 我的解题思路:
* 1.借用一个辅助栈来实现,分析可知,popV的第一个元素为pushV第一个pop的元素
* 2.遍历pushV,判断元素是否与popV第一个元素相同
* 3.若相同,则执行popV.pop(),被移除的元素顺序一定正确
* 4.若不同,则将元素加入辅助栈中
* 5.遍历完成后,比较辅助栈和popV剩余元素是否互为出入栈规则
* 注:由于步骤3中已将中途pop的元素移除,剩下的元素一定是按照全部push再全部pop的顺序执行
*
* @param {*} pushV
* @param {*} popV
*/
function IsPopOrder(pushV, popV)
{
// write code here
const array = [];
pushV.forEach(item => item === popV[popV.length - 1] ? popV.pop() : array.push(item));
return array.reverse().join(',') === popV.join(',');
}
/**
* 评论区TOP的解题思路:
* 1.核心思路也是使用辅助栈来实现
* 2.从0遍历pushV,将元素加入到辅助栈中
* 3.从0遍历popV,判断popV中元素与辅助栈最后一个元素是否相同
* 4.若相同,则移除辅助栈中最后一个元素,继续遍历popV,若不同,继续遍历pushV
* 5.两层循环结束后,根据辅助栈是否为空来确定结果
*
* @param {*} pushV
* @param {*} popV
*/
function topIsPopOrder(pushV, popV)
{
// write code here
const array = [];
let j = 0;
pushV.forEach(item => {
array.push(item);
while (j < popV.length && popV[j] === array[array.length - 1]) {
array.pop();
j++;
}
});
return !array.length;
}
function IsPopOrder(pushV, popV)
{
// write code here
if(pushV.length === 0 || popV.length === 0) return false;//边界判断
const s = [];//辅助栈
for(var i=0;i<pushV.length;i++){//以入栈为长度
s.push(pushV[i]);//将入栈一个一个读入辅助栈
while( s.length && s[s.length-1] === popV[0]){//当辅助栈不为空且辅助栈顶===出栈栈顶
s.pop();//将辅助顶弹出
popV.shift();//弹出出栈栈顶 然后继续比较直到栈顶不相等或者辅助栈为空
}
}//遍历结束
return s.length === 0;//辅助栈为空即为真
}
module.exports = {
IsPopOrder : IsPopOrder
};
class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { if(pushV.size() == 0) return false; vector<int> stack; for(int i = 0,j = 0 ;i < pushV.size();){ stack.push_back(pushV[i++]); while(j < popV.size() && stack.back() == popV[j]){ stack.pop_back(); j++; } } return stack.empty(); } };