LeetCode 20. 有效的括号
解题思路
本题要求判断字符串s是否满足括号对齐的要求,即左括号 和 右括号必须要是相同的类型,且顺序一致。卡哥整理了三种情况:
其实本题可以看作“消消乐”, 当遇到左括号时需要保存,当遇到右括号时再考虑是否要进行消除操作。另外,可以发现消除操作只和最后一次保存的状态有关,所以我们需要用到栈这一数据结构(后进先出)。
另外,本题一个小技巧就是进栈的内容。 通常,我们遇到左括号时会直接进行入栈的操作,但是在遇到右括号的时候进行消除判断就比较复杂, 代码如下:
for(int i = 0; i<s.size(); i++)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
st.push(s[i]);
}
else if(s[i] == ')')
{
if( st.empty() || st.top() != '(' )
{
st.push(s[i]);
}
else
{
st.pop();
}
}
}
代码解析
class Solution {
public:
bool isValid(string 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(']');
}
else if(!st.empty() && s[i] == st.top())
{
cout << s[i] << st.top();
st.pop();
}
else
{
return false;
}
}
return st.empty();
}
};
LeetCode 1047. 删除字符串中的所有相邻重复项
解题思路
本题还是利用栈(后进先出)的性质,如果遇到不重复的字母,直接压入栈中,如果下一个字母和栈顶元素相同,直接移除栈顶元素即可。 另外需要注意,本题要求返回的是字符串类型,一开始处理好的数据保存在栈中,简单出栈的话得到的结果是逆序的,还需要做一步反转的操作。
代码解析
class Solution {
public:
string removeDuplicates(string s) {
stack<char> st;
for(int i=0; i< s.size(); i++)
{
if(st.empty())
{
st.push(s[i]);
}
else if(s[i] != st.top())
{
st.push(s[i]);
}
else
{
st.pop();
}
// cout << st.top();
}
// return s;
string result="" ;
while(!st.empty())
{
result +=st.top();
st.pop();
}
// reverse 无返回值 不能直接return
reverse(result.begin(), result.end());
return result;
// return result;
}
};
LeetCode 150. 逆波兰表达式求值
解题思路
逆波兰表达式:是一种后缀表达式,即指运算符写在后面。在遇到运算符之前,所有的数字都需要进行保存。遇到运算符之后,从栈中拿出两个元素进行计算,计算完之后还需要将结果压入栈中,以便下一次计算。所以有几个地方需要格外注意:
- 由于运算结果也需要保存,所以栈里面的元素类型设为string ,需要二次转换。所以在一开始进行数据入栈操作的时候,就考虑进行数据类型转化操作;
- 本题数据运算的类型设为 long long。 C++中字符串转化为long long的 函数是stoll;
代码解析
class Solution {
public:
// 一次计算完还需要再压入栈中
// 如何进行类型转化?
int evalRPN(vector<string>& tokens) {
stack<long long> st;
for(int i = 0; i<tokens.size(); i++)
{
if(tokens[i] == "+")
{
long long temp1 = st.top();
st.pop();
long long temp2 = st.top();
st.pop();
st.push(temp2 + temp1);
}
else if(tokens[i] == "-")
{
long long temp1 = st.top();
st.pop();
long long temp2 = st.top();
st.pop();
st.push(temp2 - temp1);
}
else if(tokens[i] == "*")
{
long long temp1 = st.top();
st.pop();
long long temp2 = st.top();
st.pop();
st.push(temp2 * temp1);
}
else if(tokens[i] == "/")
{
long long temp1 = st.top();
st.pop();
long long temp2 = st.top();
st.pop();
st.push(temp2 / temp1);
}
else
{
st.push(stoll(tokens[i]));
}
}
int result = 0;
result = st.top();
return result;
// stack<string> st;
// int result = 0;
// for(int i = 0; i<tokens.size(); i++)
// {
// if(tokens[i] == "+")
// {
// string temp1 = st.top();
// st.pop();
// string temp2 = st.top();
// st.pop();
// result += stoll(temp2) + stoll(temp1) ;
// }
// else if(tokens[i] == "-")
// {
// string temp1 = st.top();
// st.pop();
// // 如何进行类型转化?
// string temp2 = st.top();
// st.pop();
// result += stoll(temp2) - stoll(temp1) ;
// }
// else if(tokens[i] == "*")
// {
// string temp1 = st.top();
// st.pop();
// // 如何进行类型转化?
// string temp2 = st.top();
// st.pop();
// result += stoll(temp2) * stoll(temp1) ;
// }
// else if(tokens[i] == "/")
// {
// string temp1 = st.top();
// st.pop();
// // 如何进行类型转化?
// string temp2 = st.top();
// st.pop();
// result += stoll(temp2) / stoll(temp1) ;
// }
// else
// {
// st.push(tokens[i]);
// }
// }
// return result;
}
};