Validate Stack Sequences

Dec 17, 2019

第41天。

今天的题目是Validate Stack Sequences:

简单题,直接模拟就好了:

bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    stack<int> st;
    int i = 0;
    for(auto &t: popped) {
        if (!st.empty() && st.top() == t) {
            st.pop();
        } else {
            while(i < pushed.size() && pushed[i] != t) {
                st.push(pushed[i]);
                i++;
            }
            if (pushed.size() == i) return false;
            i++;
        }
    }
    return true;
}
LeetCodeLeetCode

Find Largest Value in Each Tree Row

Network Delay Time

comments powered by Disqus