1. 程式人生 > >LeetCode周賽#112 Q2 Validate Stack Sequences

LeetCode周賽#112 Q2 Validate Stack Sequences

問題描述

 946. Validate Stack Sequences

Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.

Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: 
true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.

Note:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed is a permutation of popped.
  4. pushed and popped have distinct values.

------------------------------------------------------------

題意

給定兩個數列pushed和popped,判斷pushed和popped是否可以按順序模擬入棧出棧過程。

------------------------------------------------------------

思路

用棧模擬操作。每次入棧一個pushed的一個數後儘量多的出棧,如果最後棧空,則返回true

------------------------------------------------------------

程式碼

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        int i = 0, j = 0, k = -1, n = pushed.size();
        vector<int> st;
        while (i < n)
        {
            st.push_back(pushed[i++]);
            k++;
            while (j < n && k >= 0)
            {
                if (st[k] == popped[j])
                {
                    st.pop_back();
                    k--;
                    j++;
                }
                else
                {
                    break;
                }
            }
        }
        return (k == -1);
    }
};

相關推薦

LeetCode#112 Q2 Validate Stack Sequences

問題描述  946. Validate Stack Sequences Given two sequences pushed and popped with distinct values, return true if and only if this could

LeetCode#107 Q2 Flip String to Monotone Increasing

題目來源:https://leetcode.com/contest/weekly-contest-107/problems/flip-string-to-monotone-increasing/ 問題描述 926. Flip String to Monotone Increasing A

LeetCode#106 Q2 Minimum Add to Make Parentheses Valid (棧)

題目來源:https://leetcode.com/contest/weekly-contest-106/problems/minimum-add-to-make-parentheses-valid/ 問題描述 921. Minimum Add to Make Parentheses Val

LeetCode#105 Q2 Maximum Sum Circular Subarray (最大連續子列和變形題)

題目來源:https://leetcode.com/contest/weekly-contest-105/problems/maximum-sum-circular-subarray/ 問題描述 918. Maximum Sum Circular Subarray Given a&nbs

LeetCode#112 Q1 Minimum Increment to Make Array Unique

題目來源:https://leetcode.com/contest/weekly-contest-112/problems/minimum-increment-to-make-array-unique/ 問題描述  945. Minimum Increment to Make Ar

LeetCode#111 Q2 Delete Columns to Make Sorted

題目來源:https://leetcode.com/contest/weekly-contest-111/problems/delete-columns-to-make-sorted/ 問題描述  944. Delete Columns to Make Sorted We ar

LeetCode#112 Q3 Most Stones Removed with Same Row or Column(連通分量)

題目來源:https://leetcode.com/contest/weekly-contest-112/problems/most-stones-removed-with-same-row-or-column/ 問題描述  947. Most Stones Removed wit

LeetCode#112 Q4 Bag of Tokens(貪心)

題目來源:https://leetcode.com/contest/weekly-contest-112/problems/bag-of-tokens/ 問題描述  948. Bag of Tokens You have an initial power P, an

LeetCode#104 Q2 Partition Array into Disjoint Intervals (列舉)

問題描述 915. Partition Array into Disjoint Intervals Given an array A, partition it into two (contiguous) subarrays left and right so tha

[Swift Weekly Contest 112]LeetCode946. 驗證棧序列 | Validate Stack Sequences

Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result o

LeetCode】946. Validate Stack Sequences 解題報告(Python)

作者: 負雪明燭 id: fuxuemingzhu 個人部落格: http://fuxuemingzhu.cn/ 目錄 題目描述 題目大意 解題方法 模擬過程 日期

112th LeetCode Weekly Contest Validate Stack Sequences

Given two sequences pushed and popped with distinct values, return true if and only if

leetcode 946 Validate Stack Sequences

leetcode 946 Validate Stack Sequences 1.題目描述 2.解題思路 (1)方法1 (2)方法2 3.Python程式碼 (1)方法1 (2)方法2

Leetcode】從contest-81開始。(一般是10個contest寫一篇文章)

Contest 81 (2018年11月8日,週四,凌晨) 連結:https://leetcode.com/contest/weekly-contest-81 比賽情況記錄:結果:3/4, ranking: 440/2797。這次題目似乎比較簡單,因為我比賽的時候前三題全做出來了(1:12:39),然後第

LeetCode#107 Q4 Minimize Malware Spread II(bfs)

題目來源:https://leetcode.com/contest/weekly-contest-107/problems/minimize-malware-spread-ii/ 問題描述 928. Minimize Malware Spread II  (This probl

LeetCode#107 Q3 Three Equal Parts

題目來源:https://leetcode.com/contest/weekly-contest-107/problems/three-equal-parts/ 問題描述 927. Three Equal Parts Given an array A of 

LeetCode#107 Q1 Long Pressed Name

題目來源:https://leetcode.com/contest/weekly-contest-107/problems/long-pressed-name/ 問題描述 925. Long Pressed Name Your friend is typing his name

LeetCode#105 Q4 Number of Music Playlists (動態規劃)

題目來源:https://leetcode.com/contest/weekly-contest-105/problems/number-of-music-playlists/ 問題描述 920. Number of Music Playlists Your music player c

LeetCode#106 Q4 Minimize Malware Spread (bfs)

題目來源:https://leetcode.com/contest/weekly-contest-106/problems/minimize-malware-spread/ 問題描述 924. Minimize Malware Spread In a network of nodes,

LeetCode#106 Q3 3Sum With Multiplicity (計數排序)

題目來源:https://leetcode.com/contest/weekly-contest-106/problems/3sum-with-multiplicity/ 問題描述 923. 3Sum With Multiplicity Given an integer array&nb