File tree 1 file changed +11
-17
lines changed
1 file changed +11
-17
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean validateStackSequences (int [] pushed , int [] popped ) {
3
- int pushStart = 0 ;
4
- int popStart = 0 ;
5
3
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 ++]);
18
12
}
19
13
}
20
- while (popStart < popped .length ) {
21
- if (stack .peek () != popped [popStart ]) {
14
+ while (poppedIdx < popped .length ) {
15
+ if (stack .peek () != popped [poppedIdx ]) {
22
16
return false ;
23
17
}
18
+ poppedIdx ++;
24
19
stack .pop ();
25
- popStart ++;
26
20
}
27
21
return true ;
28
22
}
You can’t perform that action at this time.
0 commit comments