<> Title Description
Enter two Integer Sequences , The first sequence represents the push order of the stack , Please judge whether the second sequence is the pop-up sequence of the stack . Assume that all the numbers of the push stack are not equal .
<> Example
For example, sequence 1,2,3,4,5 Is the push order of a stack , sequence 4,5,3,2,1 Is a pop-up sequence corresponding to the stack pressing sequence , but 4,3,5,1,2 It can't be the pop-up sequence of the stack pressing sequence .( be careful : The length of these two sequences is equal )
<> Solutions to problems
In this paper, we only need to use an auxiliary stack to simulate the push and pop of the stack , If two sequences can be successfully executed , We can prove that the pop-up sequence corresponds to the press in sequence .
<>Code
function IsPopOrder(pushV, popV) { // write code here if(pushV.length==0)
return true; let stack=[]; let t=0; for(let i=0;i<pushV.length;i++){ stack.push(
pushV[i]); while(stack.length&&stack[stack.length-1]==popV[t]){ t++; stack.pop()
; } } return stack.length==0; }
Operating environment :JavaScript (V8 6.0.0)
Running time :14ms
Occupied memory :5324k
Technology
Daily Recommendation