1.有效的括号
给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
输入:s = “()”
输出:true
输入:s = “()[]{}”
输出:true
输入:s = “(]”
输出:false
第一种情况:已经遍历完了字符串,但是栈不为空,说明有相应的左括号没有右括号来匹配,所以return false
第二种情况:遍历字符串匹配的过程中,发现栈里没有要匹配的字符。所以return false
第三种情况:遍历字符串匹配的过程中,栈已经为空了,没有匹配的字符了,说明右括号没有找到对应的左括号return false
代码
class Solution {
public:
bool isValid(string s) {
stack<int> 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();
}
};
2. 后缀表达式
根据 逆波兰表示法,求该后缀表达式的计算结果。
有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
说明:
整数除法只保留整数部分。
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
输入:tokens = [“2”,”1”,”+”,”3”,”*”]
输出:9
解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
思路:
1.从左到右扫描每一个元素。
2.若扫描到操作数,则压入栈中,并回到1,否则执行3
3.扫描到运算符,弹出栈中两个元素,进行运算后,将新算出的数重新压入栈中,先弹出来的操作数为后操作数
代码
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> a;
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]=="+"||tokens[i]=="-"||tokens[i]=="*"||tokens[i]=="/")
{
int num1=a.top();
a.pop();
int num2=a.top();
a.pop();
if(tokens[i]=="+")a.push(num1+num2);
if(tokens[i]=="-")a.push(num2-num1);
if(tokens[i]=="*")a.push(num1*num2);
if(tokens[i]=="/")a.push(num2/num1);
}
else
{
a.push(stoi(tokens[i]));
}
}
return a.top();
}
};
3. 用栈实现队列
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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> stin;
stack<int> stout;
MyQueue() {
}
void push(int x) {
stin.push(x);
}
int pop() {
if(stout.empty())
{
while(!stin.empty())
{
stout.push(stin.top());
stin.pop();
}
}
int result = stout.top();
stout.pop();
return result;
}
int peek() {
int x=pop();
stout.push(x);
return x;
}
bool empty() {
return stin.empty()&&stout.empty();
}
}
4. 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
[“MyStack”, “push”, “push”, “top”, “pop”, “empty”]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
代码
class MyStack {
public:
queue<int> q1;
queue<int> q2; //备份
MyStack() {
}
void push(int x) {
q1.push(x);
}
int pop() {
int n=q1.size();
n--;
while(n--) //q1队列留下最后一个元素,就是此时出栈的元素
{
q2.push(q1.front());
q1.pop();
}
int result=q1.front();
q1=q2;
while(!q2.empty())
{
q2.pop();
}
return result;
}
int top() {
int x=pop();
push(x);
return x;
}
bool empty() {
return q1.empty();
}
};
5.栈序列合法性
给定 pushed-进栈顺序 和 popped-给定的出栈顺序 两个序列,每个序列中的值都不重复,判断给出的出栈序列是否合法。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。
代码
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
stack<int> s;
int n = pushed.size();
for(int i = 0,j=0; i < n; i++){//模拟进栈
s.push(pushed[i]);//先直接进栈
//然后根据出栈序列决定是否出栈
while(!s.empty()&&j<n&&s.top()==popped[j]) s.pop(),j++;//出栈条件
}
return s.empty() ? true:false;//若合法,则此时栈一定是空的
}