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:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
is a permutation ofpopped
.pushed
andpopped
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