20 有效括号 | 1047. 删除字符串中的所有相邻重复项 | 150. 逆波兰表达式求值

56 阅读1分钟

有效括号

简单

题目描述:给定一个只包含 '('')''{''}''['']' 的字符串 s,判断字符串是否有效。

代码

 class Solution {
 public:
     bool isValid(string s) {
         int n = s.length();
         if (n % 2 != 0)
             return false;
         stack<char> st;
         for (int i = 0; i < n; i++)
         {
             if (s[i] == '(' || s[i] == '{' || s[i] == '[')
                 st.push(s[i]);
             if (st.empty() == 1)
                 return false;
             if (s[i] == ')')
             {
                 char ch = st.top();
                 st.pop();
                 if (ch != '(')
                     return false;
             }
             else if (s[i] == '}')
             {
                 char ch = st.top();
                 st.pop();
                 if (ch != '{')
                     return false;
             }
             else if (s[i] == ']')
             {
                 char ch = st.top();
                 st.pop();
                 if (ch != '[')
                     return false;
             }
         }
         if (st.empty() == 1)
             return true;
         else    
             return false;
     }
 };

删除字符串中的所有相邻重复项

简单

题目描述:给定一个由小写字符组成的字符串 s,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在 s 上反复执行重复项删除操作,直到无法继续删除。

思路

主要是想到利用栈来寻找是否有重复项,如果单纯删除一次,不需要利用栈,循环一遍即可,而本题要求重复删除,所以需要用到栈。

代码

 class Solution {
 public:
     string removeDuplicates(string s) {
         stack<char> st;
         int n = s.size();
         for (int i = 0; i < n; i++)
         {
             if (st.empty() == 1)
                 st.push(s[i]);
             else
             {
                 char ch = st.top();
                 st.pop();
                 if (ch != s[i])
                 {
                     st.push(ch);
                     st.push(s[i]);
                 }
             }
         }  
         int w = st.size();
         string result(w, '\0');
         for (int i = w - 1; i >= 0; i--)
         {
             char ch = st.top();
             st.pop();
             result[i] = ch;
         }
         return result;
     }
 };

逆波兰表达式求值

简单

题目描述:给定一个字符串数组 tokens,表示一个根据逆波兰表示法表示的算数表达式,计算该表达式并返回结果。

代码

 class Solution {
 public:
     int evalRPN(vector<string>& tokens) {
         stack<long long> st;
         int n = tokens.size();
         for (int i = 0; i < n; i++)
         {
             long long k1, k2;
             if (tokens[i] == "+")
             {
                 k1 = st.top();
                 st.pop();
                 k2 = st.top();
                 st.pop();
                 st.push(k2 + k1);
             }
             else if (tokens[i] == "-")
             {
                 k1 = st.top();
                 st.pop();
                 k2 = st.top();
                 st.pop();
                 st.push(k2 - k1);
             }
             else if (tokens[i] == "*")
             {
                 k1 = st.top();
                 st.pop();
                 k2 = st.top();
                 st.pop();
                 st.push(k2 * k1);
             }
             else if (tokens[i] == "/")
             {
                 k1 = st.top();
                 st.pop();
                 k2 = st.top();
                 st.pop();
                 st.push(k2 / k1);
             }
             else
             {
                 st.push(stoll(tokens[i]));  // 将string转成long long 型
             }
         }
         int x = st.top();
         return x;
     }
 };