栈与队列总结1
1.有效的括号
https://leetcode-cn.com/problems/valid-parentheses/
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合
class Solution {
public:
bool isValid(string s) {
int n = s.length();
if(n==0 || n%2!=0) return false;
unordered_map<char,char> pairs = {
{')','('},
{']','['},
{'}','{'}
};
stack<char> stk;
for(char ch:s){
if(pairs.count(ch)){
if(stk.empty() || stk.top()!=pairs[ch]){
return false;
}
stk.pop();
}
else{
stk.push(ch);
}
}
return stk.empty();
}
};
- 时间复杂度:O(n),其中 n 是字符串 s 的长度。
- 空间复杂度:O(n+∣Σ∣),其中 Σ 表示字符集,本题中字符串只包含 6种括号,∣Σ∣=6。栈中的字符数量为 O(n),而哈希表使用的空间为 O(∣Σ∣),相加即可得到总空间复杂度。
利用栈和哈希表
2.删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/
class Solution {
public:
string removeOuterParentheses(string S) {
string res;
int n = S.length();
stack<char> stk;
int left=0;
for(int i=0;i<n;i++){
if(S[i]==')'){
stk.pop();
if(!stk.empty()){
res.push_back(S[i]);
}else{
left = i+1;
}
}else{
stk.push(S[i]);
if(i!=left){
res.push_back(S[i]);
}
}
}
return res;
}
};
- 时间复杂度 o(n)
- 空间复杂度 o(n)
注:c++ 中 string 本身就提供【入栈】和【出栈】的接口
push_back ------------push
pop_back---------------pop
back----------------------top
3.最小栈
https://leetcode-cn.com/problems/min-stack/
class MinStack {
public:
/** initialize your data structure here. */
stack<int> x_stack;
stack<int> min_stack;
MinStack() {
min_stack.push(INT_MAX);
}
void push(int val) {
x_stack.push(val);
if(val<min_stack.top()){
min_stack.push(val);
}else{
min_stack.push(min_stack.top());
}
}
void pop() {
x_stack.pop();
min_stack.pop();
}
int top() {
return x_stack.top();
}
int getMin() {
return min_stack.top();
}
};
- 时间复杂度 o(1)
- 空间复杂度 o(n)
需要借助一个辅助栈,辅助栈构建的细节要很懂
4.下一个更大元素I
https://leetcode-cn.com/problems/next-greater-element-i/
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
if(nums1.size()==0 || nums2.size()==0) return res;
//pairs 中存储的是数,以及其对应的下一个更大的值
unordered_map<int,int> pairs;
stack<int> stk;
for(int i=0;i<nums2.size();i++){
while(!stk.empty() && nums2[i]>stk.top()){
pairs[stk.top()] = nums2[i];
stk.pop();
}
stk.push(nums2[i]);
}
while(!stk.empty()){
pairs[stk.top()]=-1;
stk.pop();
}
for(int i=0;i<nums1.size();i++){
res.push_back(pairs[nums1[i]]);
}
return res;
}
};
- 时间复杂度 o(m+n)
- 空间复杂度 o(n) -----栈的构建
借助了哈希表,以及构建栈存储下一个更大元素的方法要懂
5.滑动窗口的最大值
剑指offer 59
https://leetcode-cn.com/problems/hua-dong-chuang-kou-de-zui-da-zhi-lcof/
给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值。
你可以假设 k 总是有效的,在输入数组不为空的情况下,1 ≤ k ≤ 输入数组的大小。
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
vector<int> res;
if(n==0) return res;
//双端队列
deque<int> q;
for(int i=0;i<k;i++){
int a = 0;
while(!q.empty() && q.back()<nums[i]){
q.pop_back();
a++;
}
for(int j=0;j<a;j++){
q.push_back(nums[i]);
}
q.push_back(nums[i]);
}
res.push_back(q.front());
q.pop_front();
for(int i=k;i<n;i++){
int a = 0;
while(!q.empty() && q.back()<nums[i]){
q.pop_back();
a++;
}
for(int j=0;j<a;j++){
q.push_back(nums[i]);
}
q.push_back(nums[i]);
res.push_back(q.front());
q.pop_front();
}
return res;
}
};
- 时间复杂度 o(n*k)
- 空间复杂度 o(k)
借助了双端队列deque进行求解,要理解双端队列里值的变化过程