龍崗做網(wǎng)站的公司源碼之家
946. 驗(yàn)證棧序列
描述 :
給定?pushed
?和?popped
?兩個(gè)序列,每個(gè)序列中的?值都不重復(fù),只有當(dāng)它們可能是在最初空棧上進(jìn)行的推入 push 和彈出 pop 操作序列的結(jié)果時(shí),返回?true
;否則,返回?false
?。
題目 :
LeetCode?946. 驗(yàn)證棧序列
題解 :?946. 驗(yàn)證棧序列 - 力扣(LeetCode)
代碼 :
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();}
}