給定棧的壓入序列判斷棧的彈出序列是否合法
附上動圖,一看就會
代碼在此:
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack =new Stack<>();
int i =0;
for(int num:pushed){
stack.push(num);
while(!stack.isEmpty() && stack.peek()==popped[i]){
stack.pop();
i++;
}
}
return stack.isEmpty();
}
}
今日纔來打卡!
五一放假玩耍了4天!!!
釘釘網課時代假中假餘額不足!!!