栈实现队列的出队操作效率低下:栈底元素(对应队首元素)无法直接删除,需要将上方所有元素出栈。所以我们一定需要两个栈,一个输入一个输出;
在push数据的时候,只要数据放进输入栈就好,但在pop的时候,操作就复杂一些,输出栈如果为空,就把进栈数据全部导入进来(注意是全部导入)再从出栈弹出数据,如果输出栈不为空,则直接从出栈弹出数据就可以了。
class MyQueue {
public:
stack<int> stin;
stack<int> stout;
MyQueue() {
}
void push(int x) {
stin.push(x);
}
int pop() {
if(stout.empty()){
//只有当stout为空时,才从stin中导入数据
while(!stin.empty()){
//从stin中导入数据直到stin为空
stout.push(stin.top());
stin.pop();
}
}
int result =stout.top();
stout.pop();
return result;
}
int peek() {
int res=this->pop();//直接使用之前写的pop
stout.push(res);//因为pop弹出了res所以再添加上
return res;
}
bool empty() {
return stin.empty()&&stout.empty();
}
};
/**
* 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();
*/
class MyStack {
public:
queue<int> q1;
queue<int> q2;
MyStack() {
}
void push(int x) {
q1.push(x);
}
int pop() {
int size =q1.size();
size--;
while(size--){
// 将que1 导入que2,但要留下最后一个元素
q2.push(q1.front());
q1.pop();
}
int result=q1.front();//取出留下的最后一个元素,最后要返回此元素
q1.pop();
q1=q2;//q2里没有最后一个元素,因此q1=q2相当于删除了最后一个元素
while(!q2.empty()){
q2.pop();//清空q2
}
return result;
}
int top() {
int size =q1.size();
size--;
while(size--){
q2.push(q1.front());
q1.pop();
}
int result=q1.front();
q2.push(q1.front());//与pop的区别就是这里将q1最后一个元素也存在了q2,不做删除元素的操作
q1.pop();
q1=q2;
while(!q2.empty()){
q2.pop();
}
return result;
}
bool empty() {
return q1.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();
*/
实际上只需要一个队列就可以模拟栈,只需要让队尾之前的元素重新排队就好了
class MyStack {
public:
queue<int> q1;
MyStack() {
}
void push(int x) {
q1.push(x);
}
int pop() {
int size =q1.size();
size--;
while(size--){
// 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾
q1.push(q1.front());
q1.pop();
}
int result=q1.front();//取出留下的最后一个元素,最后要返回此元素
q1.pop();
return result;
}
int top() {
int size =q1.size();
size--;
while(size--){
q1.push(q1.front());
q1.pop();
}
int result=q1.front();
q1.push(q1.front());
q1.pop();
return result;
}
bool empty() {
return q1.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();
*/
我自己的代码太丑陋了就不贴了,贴一下卡哥的代码吧。
我是所有情况讨论了,然后用一个index判断目前是不是还有左括号没有匹配
class Solution {
public:
bool isValid(string s) {
if (s.size() % 2 != 0) return false; // 如果s的长度为奇数,一定不符合要求
stack<char> st;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') st.push(')');
else if (s[i] == '{') st.push('}');
else if (s[i] == '[') st.push(']');
// 第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号 return false(右括号多了)
// 第二种情况:遍历字符串匹配的过程中,发现栈里没有我们要匹配的字符。所以return false
else if (st.empty() || st.top() != s[i]) return false;//括号相互交叉了
else st.pop(); // st.top() 与 s[i]相等,栈弹出元素
}
// 第一种情况:此时我们已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false,否则就return true(左括号多了)
return st.empty();
}
};
注意这个题要两个两个删除
class Solution {
public:
string removeDuplicates(string S) {
string result;//这里string可以自己做栈,string有自己的pop和push接口
for(char s : S) {
if(result.empty() || result.back() != s) {
result.push_back(s);
}
else {
result.pop_back();
}
}
return result;
}
};