946. Validate Stack Sequences 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617class Solution {public: bool validateStackSequences(vector<int>& pushed, vector<int>& popped) { int n = pushed.size(), i=0, j=0; stack<int> sta; while(i<n && j<n){ if(pushed[i] == popped[j]){i++;j++;} else sta.push(pushed[i++]); // 檢查當前j 指向的值是否為stack.top() while(j<n && !sta.empty() && sta.top() == popped[j]){ sta.pop(); j++; } } return j==n && sta.empty(); }}; analysis time complexity O(n) space complexity O(n)