三题训练,加深对栈与队列的影响
http://L20. 有效的括号https://leetcode.cn/problems/valid-parentheses/
一开始自己思考了一种方法,其在力扣上也能跑通的,但代码长度有点长
class Solution {
public:
bool isValid(string s)
{
stack<char>result;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
{
result.push(s[i]);
}
else if (s[i] == ')')
{
if (result.empty())
return false;
else if (result.top() == '(')
result.pop();
else
return false;
}
else if (s[i] == '}')
{
if (result.empty())
return false;
else if (result.top() == '{')
result.pop();
else
return false;
}
else if (s[i] == ']')
{
if (result.empty())
return false;
else if (result.top() == '[')
result.pop();
else
return false;
}
}
if (!result.empty())
return false;
else return true;
}
};
再看过卡哥代码讲解之后,才发现好的算法能让自己的代码变的漂亮起来,也就是对算法精度的理解,只要遇到左边括号,就插入一个右括号,若遇到一个右括号,判断其是否等于栈顶的右括号,若相等,就将其弹出,若不等则报错,以此反复
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();
}
};
知道今天安排的第一题如何写,后面的也就游刃有余,依葫芦画瓢
http://● 1047. 删除字符串中的所有相邻重复项https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
class Solution {
public:
string removeDuplicates(string s) {
string sol;
deque<char>result;
for (int i = 0; i < s.size(); i++)
{
if (result.empty() || result.back() != s[i])
result.push_back(s[i]);
else if (result.back() == s[i])
{
result.pop_back();
}
}
while (!result.empty())
{
sol += result.front();
result.pop_front();
}
return sol;
}
};
http://150. 逆波兰表达式求值https://leetcode.cn/problems/evaluate-reverse-polish-notation/
本题也一样,只要理解题目要求就好,遇到符号就取两个元素进行运算
class Solution {
public:
int evalRPN(vector<string>& tokens)
{
stack<long long>temp;
if (tokens.empty())//如果为空,就返回0
return 0;
for (int i = 0; i < tokens.size(); i++)
{
if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/")
{
temp.push(stoll(tokens[i]));
}
else
{
long cal1 = temp.top();
temp.pop();
long cal2 = temp.top();
temp.pop();
if (tokens[i] == "+")
temp.push(cal1 + cal2);
if(tokens[i] == "-")
temp.push(cal2 - cal1);
if(tokens[i] == "*")
temp.push(cal2 * cal1);
if(tokens[i] == "/")
temp.push(cal2 / cal1);
}
}
int aaa = temp.top();
temp.pop();
return aaa;
}
};