File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11class Solution {
22 public boolean validateStackSequences (int [] pushed , int [] popped ) {
3- int pushStart = 0 ;
4- int popStart = 0 ;
53 Stack <Integer > stack = new Stack <>();
6- while (pushStart < pushed .length && popStart < popped .length ) {
7- if (stack .isEmpty ()) {
8- stack .push (pushed [pushStart ++]);
9- }
10- else {
11- if (stack .peek () == popped [popStart ]) {
12- stack .pop ();
13- popStart ++;
14- }
15- else {
16- stack .push (pushed [pushStart ++]);
17- }
4+ int pushedIdx = 0 ;
5+ int poppedIdx = 0 ;
6+ while (pushedIdx < pushed .length && poppedIdx < popped .length ) {
7+ if (!stack .isEmpty () && stack .peek () == popped [poppedIdx ]) {
8+ stack .pop ();
9+ poppedIdx ++;
10+ } else {
11+ stack .push (pushed [pushedIdx ++]);
1812 }
1913 }
20- while (popStart < popped .length ) {
21- if (stack .peek () != popped [popStart ]) {
14+ while (poppedIdx < popped .length ) {
15+ if (stack .peek () != popped [poppedIdx ]) {
2216 return false ;
2317 }
18+ poppedIdx ++;
2419 stack .pop ();
25- popStart ++;
2620 }
2721 return true ;
2822 }
You can’t perform that action at this time.
0 commit comments