232. 用栈实现队列
问题描述:传送门
思路:
1、双栈一进一出 + this指针复用
①入队,就是满足stIn栈进元素就行。
②出队,stOut要为空,此时如果stIn不为空,要全部取头一个一个转移过去。最后清空stOut.
③Peek函数要学会利用this指针实现复用。
④empty, 返回两个空栈的真值就行。
代码:
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
while(stOut.empty()){
while(!stIn.empty()){
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int x =this->pop();//直接调用当前函数
stOut.push(x);//因为要取表头的这个元素,被pop弹出去了
return x;//最后返回这个变量就行
}
/** Returns whether the queue is empty. */
bool empty() {
return stOut.empty()&&stIn.empty();
// 这种写法太幼稚了
// if(stOut.empty()&&stIn.empty()){
// return true;
// }else
// return false;
// }
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
225. 用队列实现栈
问题描述:传送门
思路:
1、一个队列解决
①入栈,就是入队
②出栈,除去最后一个元素后,将元素放到队尾,然后出队头元素
③取栈顶元素,出队,x变量存出去的队头元素,return x
④判空,就是判队列的空
代码:
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size =que.size();
size--;
while(size--){//这里控制循环次数
//将队列头部的元素(除了最后一个元素外)重新添加到队列尾部
que.push(que.front());//将队头的元素放入队尾
que.pop();//弹出队头元素
}
int result = que.front();//此时就是去栈的元素
que.pop();//栈顶元素被取出来了
return result;
}
/** Get the top element. */
int top() {
return que.back();//就是求队尾元素
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/
155. 最小栈
问题描述:传送门
思路:
1、辅助栈解决
①初始的时候,辅助栈记得放一个INT_MAX;
②栈每放入一个元素,辅助栈记得进行最小值比较后,再放入。
③栈和辅助栈一一对应,除了top的时候取的是栈的元素。
class MinStack {
public:
stack<int> s;//创建栈
stack<int> mins;//旁边开一个最小值栈
/** initialize your data structure here. */
MinStack() {
mins.push(INT_MAX);//初始化最小值栈,栈顶先放一个INT_MAX;
}
void push(int x) {
s.push(x);
mins.push(min(mins.top(),x));
}
void pop() {
s.pop();
mins.pop();
}
int top() {
return s.top();//从原栈取栈顶元素
}
int getMin() {
return mins.top();
}
};
20. 有效的括号
问题描述:传送门
思路:
1、哈希表1
(
难
点
)
\color{red}{(难点)}
(难点)(彩色标记技巧)
①掌握for(char& ch:s)的区别
②for(char c: s.toCharArray()): 是for(;;)的形式,char[] cs = s.toCharArray(); for(int i=0;i<cs.length;i++){ char c = cs[i]; }
2、栈
class Solution {
public:
bool isValid(string s) {
int n= s.size();
if(n%2 ==1){
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();
}
};
739. 每日温度
问题描述:传送门
思路:
1、递减栈
①先把这些数的距离用vector数组统一设置成0,且遍历一遍这个数组
②当栈为空,返回空数组。当栈不为空,且要插入的数组值大于栈顶值,栈顶值出栈,更新vector ans。
③放入插入的数组。
④返回动态数组ans.
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& t) {
int n =t.size();
vector<int> ans(n,0);//n个数组,初值为0
stack<int> s;
for(int i=0;i<n;i++){
while(!s.empty()&&t[i]>t[s.top()]){
int t =s.top();//取栈顶元素
s.pop();
ans[t]=i-t;//第一个的值,等于当前元素减去第一个
}
s.push(i);//栈空等情况,先入栈再说
}
return ans;
}
};
2、逆序存放元素栈(选高矮)
①从后往前把元素的下标放入栈中,栈为空,置为0
②当栈不为空,且栈中元素小于等于 选取元素 时,去掉栈中的“矮个子”,更新选取元素到栈中。
参考文献
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& t) {
int n =t.size();
vector<int> ans(n,0);//n个数组,初值为0
stack<int> s;
for(int i=n-1;i>=0;i--){
while(!s.empty()&&t[s.top()]<=t[i]){
s.pop();//栈不为空,且当前栈的最大值小于选取最大值。
}
ans[i] = s.empty()?0:s.top()-i;//更新答案
s.push(i);//放入数字
}
return ans;
}
};
503. 下一个更大元素 II
问题描述:传送门
思路:
2、逆序存放元素栈(选高矮)
①把当前元素乘两倍,存数的时候,记得取余就行。
参考文献
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& c) {
int n =c.size();
vector<int> ans(n);
stack<int> s;
for(int i=n*2-1;i>=0;i--){
while(!s.empty()&&s.top()<=c[i%n]){
s.pop();
}
ans[i%n] = s.empty()?-1:s.top();
s.push(c[i%n]);
}
return ans;
}
};
彩色标记大法 (https://blog.csdn.net/COCO56/article/details/105155328/) ↩︎