232.用栈实现队列
思路一:直接通过压出来找第一个,感觉有点冗余
class MyQueue {
public:
stack<int>s1,s2;
MyQueue() {
}
void swapStack(stack<int>&t1,stack<int>&t2)
{
while(!t1.empty())
{
int mid=t1.top();
t1.pop();
t2.push(mid);
}
}
void push(int x) {
s1.push(x);
}
int pop() {
swapStack(s1,s2);
int mid=s2.top();
s2.pop();
swapStack(s2,s1);
return mid;
}
int peek() {
swapStack(s1,s2);
int mid=s2.top();
swapStack(s2,s1);
return mid;
}
bool empty() {
if(s1.empty() && s2.empty())
return true;
return false;
}
};
二刷:
思路:压入栈和弹出栈(题目保证每一次调用都有效)
- 压入的时候把弹出栈中的元素全部放到压入栈中
- 弹出的时候把压入栈中的元素全部放入弹出栈中
class MyQueue {
public:
stack<int>input,output;
int len;
MyQueue() {len=0; }
void push(int x) {
while(!output.empty()){
int mid=output.top();
output.pop();
input.push(mid);
}
input.push(x);
len++;
}
int pop() {
while(!input.empty()){
int mid=input.top();
input.pop();
output.push(mid);
}
int res=output.top();
output.pop();
len--;
return res;
}
int peek() {
while(!input.empty()){
int mid=input.top();
input.pop();
output.push(mid);
}
return output.top();
}
bool empty() {
if(len==0) return true;
return false;
}
};
225.用队列实现栈
思路一:双队列实现
class MyStack {
public:
queue<int>q1,q2;
MyStack() {
}
int swapQue(queue<int>&e1,queue<int>&e2)
{
while(!e1.empty())
{
if(e1.size()==1)
{
return e1.front();
}
int mid=e1.front();
e1.pop();
e2.push(mid);
}
return -1;
}
void push(int x) {
q1.push(x);
}
int pop() {
int mid=swapQue(q1,q2);
q1.pop();
int mid2=swapQue(q2,q1);
if(mid2>0)
{
q2.pop();
q1.push(mid2);
}
return mid;
}
int top() {
int mid=swapQue(q1,q2);
q2.push(mid);
q1.pop();
int mid2=swapQue(q2,q1);
if(mid2>0)
{
q1.push(mid2);
q2.pop();
}
return mid;
}
bool empty() {
if(q1.empty() && q2.empty())
return true;
return false;
}
};
思路二:一个队列也能实现
class MyStack {
public:
//一个队列实现栈
queue<int>q1;
MyStack() {
}
void push(int x) {
q1.push(x);
}
int pop() {
int n=q1.size();
while(n>1)
{
int mid=q1.front();
q1.pop();
q1.push(mid);
n--;
}
int result=q1.front();
q1.pop();
return result;
}
int top() {
int n=q1.size();
while(n>1)
{
int mid=q1.front();
q1.pop();
q1.push(mid);
n--;
}
int result=q1.front();
q1.pop();
q1.push(result);
return result;
}
bool empty() {
if(q1.empty())
return true;
return false;
}
};
简单复习了一下字符串和数组的知识,对替换空格进行了知识补充
二刷:
思路一:同样思路,创建一个交换函数,每次在被转换的栈中留一个元素
class MyStack {
public:
queue<int>input,output;
int len;
MyStack() {
len=0;
}
void swapQue(queue<int>&q1,queue<int>&q2){
while(q1.size()>1){
int mid=q1.front();
q1.pop();
q2.push(mid);
}
}
void push(int x) {
input.push(x);
len++;
}
int pop() {
swapQue(input,output);
int res=input.front();
swapQue(output,input);
int mid=output.front();
output.pop();
input.push(mid);
len--;
return res;
}
int top() {
return input.back();
}
bool empty() {
if(len==0) return true;
return false;
}
};
剑指 Offer 31 . 栈的压入、弹出序列(判断出栈合法性)
思路:遍历每一个出栈的数字,遍历到i时,就判断栈中是否入栈到i。
1.还没有入栈到i,先入栈到i,此时出栈合法
2.已经入栈到i后面,判断栈顶元素是否等于出栈元素
-
等于时出栈合法
-
不等于时出栈不合法
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> st;
int n=pushed.size();
for(int i=0,j=0;i<n;i++)
{
st.emplace(pushed[i]);//模拟入栈
while(!st.empty() && st.top()==popped[j])//如果栈顶元素跟出栈元素相同并且st不为空,此时可以出栈
{
st.pop();
j++;//相当于遍历出栈顺序
}
}
return st.empty();
}
};
二刷:
思路:遍历出栈序列和入栈序列
- 栈顶跟出栈元素不匹配时,直接按入栈序列入栈
- 当匹配时,按出栈序列出栈
- 最后栈空的话出栈顺序合法
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int i=0,j=0,n=pushed.size();
stack<int>st;
while(j<n){
while(!st.empty() && st.top()==popped[j]){
st.pop();
j++;
}
if(i>=n) break;
st.push(pushed[i++]);
}
return st.empty();
}
};