代码随想录DAY11-栈和队列

79 阅读1分钟

20 有效的括号

class Solution {
 public:
  bool isValid(string s) {
    stack<char> stk;
    for(int i = 0; i < s.size(); i++){
      if(stk.empty() || !isMatched(stk.top(), s[i])){
        stk.push(s[i]);
      }else{
          stk.pop();
      }
    }
    return stk.empty();
  }
  
 private:
  bool isMatched(char a, char b){
    if((a == '(' && b == ')') ||
    (a == '{' && b == '}') ||
    (a == '[' && b == ']')){
        return true;
    }
    return false;
  }
};

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

class Solution {
 public:
  string removeDuplicates(string s) {
    stack<char> stk;
    for(int i = 0; i < s.size(); i++){
      if(!stk.empty() && stk.top() == s[i]){
        stk.pop();
      }else{
        stk.push(s[i]);
      }
    }
    string ret;
    while(!stk.empty()){
      ret.push_back(stk.top());
      stk.pop();
    }
    reverse(ret.begin(), ret.end());
    return ret;
  }
};

reverse(ret.begin(), ret.end())

for(char s: S){
​
}

150 逆波兰表达式求值

class Solution {
 public:
  int evalRPN(vector<string>& tokens) {
    stack<string> stk;
    for(string s: tokens){
      if(isInteger(s)){
        stk.push(s);
      }else{
        int x = stoi(stk.top());
        stk.pop();
        int y = stoi(stk.top());
        stk.pop();
        int ret;
        int flag = flagCal(s);
        switch(flag){
          case 0:
            ret = x + y;
            break;
          case 1:
            ret = y - x;
            break;
          case 2:
            ret = x * y;
            break;
          case 3:
            ret = y / x;
            break;
          default:
            break;
        }
        stk.push(to_string(ret));
      }
    }
    return stoi(stk.top());
  }
​
 private:
  bool isInteger(string s){
    if(s.size() == 1 && s[0] == '-') return false;
    if(s[0] != '-' && !isdigit(s[0])) return false;
    for(char c: s.substr(1)){
      if(!isdigit(c)){
        return false;
      }
    }
    return true;
  }
​
  int flagCal(string s){
    if(s == "+"){
      return 0;
    }else if(s == "-"){
      return 1;
    }else if(s == "*"){
      return 2;
    }else{
      return 3;
    }
  }
};

出错:

isDigit:

- 单独

-987 负数的判断

代码写的太长了

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;
    }
};

可以不用判断isInteger,不是符号就是整数

st.push(num1 + num2)

可以直接push,不用再设置一个变量去存储