题目
20. 有效的括号
自己想法及注意点
我的想法采用数组或集合记录不同类型括号的数量,如果最后括号数量不对应,则输出false。但是本题要考虑括号的顺序,单纯计数会出现s ="([)]"这种情况是通不过的。
更新:检测到左括号入栈,检测到右括号检查栈顶元素,对应上即出栈,否则return false.循环完后判断栈是否为空。
class Solution {
public:
bool isValid(string s) {
//检测到左括号入栈,检测到右括号检查栈顶元素,对应上即出栈,否则return false
stack<int> ms;
for(char i:s){
if(i=='(' || i=='[' || i=='{')
ms.push(i);
else if(i==')'){
if(ms.empty()==false){
char temp = ms.top();
if(temp=='(') ms.pop();
else return false;
}
else return false;
}
else if(i==']'){
if(ms.empty()==false){
char temp = ms.top();
if(temp=='[') ms.pop();
else return false;
}
else return false;
}
else if(i=='}'){
if(ms.empty()==false){
char temp = ms.top();
if(temp=='{') ms.pop();
else return false;
}
else return false;
}
}
//循环完后判断栈是否为空
if(ms.empty()==true) return true;
else return false;
}
};
栈的方法
三种括号不匹配情况分析:
- 字符串里左方向的括号多余了 ,所以不匹配。
- 括号没有多余,但是 括号的类型没有匹配上。
- 字符串里右方向的括号多余了,所以不匹配。
解题思路:
- 如果字符串大小是奇数,则一定不满足括号对应;
- 遍历字符串s;
- ⭐遇到左括号,则在栈里面存入对应的右括号;
- 遇到右括号,则与栈顶元素进行对比;⭐如果栈为空,表明一开始就没有左括号,则直接返回false,如果当前右括号与栈顶元素不匹配,也返回false。
- 最后返回栈是否为空,为空则说明全部括号匹配完毕。
代码
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();
}
};
1047. 删除字符串中的所有相邻重复项
思路
- 遍历字符串s;
- 用栈存放遍历过的元素,当遍历当前的这个元素的时候,去栈里看一下是不是遍历过相同数值的相邻元素。
- 如果遍历过,当前元素不放入栈内且弹出栈顶元素。
- 最后返回栈内元素反转后的字符串。
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
string result="";
for(char i:s){
if(st.empty() || st.top()!= i) st.push(i);
else st.pop();
}
while(st.empty()==false){
result += st.top();
st.pop();
}
reverse(result.begin(),result.end());
return result;
}
};
150. 逆波兰表达式求值
逆波兰表达式
-
逆波兰表达式:是一种后缀表达式,所谓后缀就是指运算符写在后面。平常使用的算式则是一种中缀表达式,如
( 1 + 2 ) * ( 3 + 4 )。该算式的逆波兰表达式写法为( ( 1 2 + ) ( 3 4 + ) * )。 -
逆波兰表达式主要有以下两个优点:
- 去掉括号后表达式无歧义,上式即便写成 1 2 + 3 4 + * 也可以依据次序计算出正确结果。
- 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。
解题思路
- 遍历字符串,碰到数字存入栈中;碰到运算符,取栈顶两个元素做运算后,将结果再存入栈中。注意减和除的操作两个元素的计算选取。
- 全部元素遍历完,取栈顶元素即为最终结果。
stol和stoll函数
- stol():此函数将在函数调用中作为参数提供的字符串转换为long int。
- stoll():此函数将在函数调用中作为参数提供的字符串转换为long long int。
C++中的单双引号
- 单引号是 char 字符类型, 双引号是 string 字符串类型。本题中tokens用的是string类型定义的,所以判断时要用双引号。
自己代码
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<long long> st;
for(int k=0;k<tokens.size();k++){
//非运算符,直接压入
if(st.empty() || (tokens[k]!="+" && tokens[k]!="-" && tokens[k]!="*" && tokens[k]!="/")) st.push(stoll(tokens[k]));
//运算符,计算
else{
if(tokens[k]=="+"){
int i = st.top();
st.pop();
int j = st.top();
st.pop();
int res = i+j;
st.push(res);
}
else if(tokens[k]=="-"){
int i = st.top();
st.pop();
int j = st.top();
st.pop();
int res = j-i;//注意要反过来
st.push(res);
}
else if(tokens[k]=="*"){
int i = st.top();
st.pop();
int j = st.top();
st.pop();
int res = i*j;
st.push(res);
}
else{
int i = st.top();
st.pop();
int j = st.top();
st.pop();
int res = j/i;//注意要反过来
st.push(res);
}
}
}
return int(st.top());
}
};
简单写法
class Solution {
public:
int evalRPN(vector<string>& tokens) {
// 力扣修改了后台测试数据,需要用longlong
stack<long long> st;
for (int i = 0; i < tokens.size(); i++) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
long long num1 = st.top();
st.pop();
long long 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(stoll(tokens[i]));
}
}
int result = st.top();
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
return result;
}
};