请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
优化:
一个队列在模拟栈弹出元素的时候只要将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部,此时在去弹出元素就是栈的顺序了。
- #include
- #include
- using namespace std;
- class Mystack
- {
- public:
- queue<int> que;
- Mystack()
- {
-
- }
- void push(int x)
- {
- que.push();
- }
- int pop()
- {
- int size=que.size();
- size--;
- while(size--)
- {
- 将队列头部的元素(除了最后一个元素外) 重新添加到队列尾部
- que.push(que.front());
- que.pop();
- }
- int result=que.front();
- que.pop();
- return result;
- }
- int top()
- {
- return que.back();
- }
- return empty()
- {
- return que.empty();
- }
-
- };
分析:
弹入:

弹出

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你 只能 使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
- class MyQueue {
- public:
- stack<int> stackin;
- stack<int> stackout;
- MyQueue() {
-
- }
-
- void push(int x) {
- stackin.push(x);
-
- }
-
- int pop() {
- if(stackout.empty())
-
- {
- while(!stackin.empty())
- {
- stackout.push(stackin.top());
- stackin.pop();
- }
-
- }
- int result=stackout.top();
- stackout.pop();
-
- return result;
-
-
- }
-
- int peek() {
- int res=this->pop();
- stackout.push(res);
- return res;
- }
-
- bool empty() {
-
- return stackin.empty()&&stackout.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();
- */
分析

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
- class Solution {
- public:
- bool isValid(string s) {
-
- if(s.size()%2!=0)return false;
- stack<char>st;
- for(int i=0;i
size();i++) - {
- if (s[i] == '(') st.push(')');
- else if (s[i] == '{') st.push('}');
- else if (s[i] == '[') st.push(']');
- else if(st.empty()||st.top()!=s[i])return false;
- else
- {
- st.pop();
- }
-
- }
- return st.empty();
-
- }
- };
栈的实现:
- class Solution {
- public:
- string removeDuplicates(string S) {
- stack<char> st;
- for (char s : S) {
- if (st.empty() || s != st.top()) {
- st.push(s);
- } else {
- st.pop(); // s 与 st.top()相等的情况
- }
- }
- string result = "";
- while (!st.empty()) { // 将栈中元素放到result字符串汇总
- result += st.top();
- st.pop();
- }
- reverse (result.begin(), result.end()); // 此时字符串需要反转一下
- return result;
-
- }
- };
其实可以直接字符串
- class Solution {
- public:
- string removeDuplicates(string S) {
- string result;
- for(char s : S) {
- if(result.empty() || result.back() != s) {
- result.push_back(s);
- }
- else {
- result.pop_back();
- }
- }
- return result;
- }
- };
- class Solution {
- public:
- int evalRPN(vector
& tokens) { - stack<int> st;
- for (int i = 0; i < tokens.size(); i++) {
- if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
- int num1 = st.top();
- st.pop();
- int num2 = st.top();
- st.pop();
- if (tokens[i] == "+") st.push(num2 + num1);
- if (tokens[i] == "-") st.push(num2 - num1);
- if (tokens[i] == "*") st.push(num2 * num1);
- if (tokens[i] == "/") st.push(num2 / num1);
- } else {
- st.push(stoi(tokens[i]));
- }
- }
- int result = st.top();
- st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
- return result;
- }
- };
代码bug:
不能区分负号和减号