代码随想录算法训练营第十一天|LeetCode20、LeetCode1047、LeetCode150

LeetCode20

题目:

 自己解法:(枚举)

class Solution {
public:
    bool isValid(string s) {
        stack<char> iss;
        int i;
        for(i = 0;i < s.size();i++)
        {
           if(s[i] == '('||s[i] == '['||s[i] == '{') 
           {
               iss.push(s[i]);
           }
            if(s[i] == ')')
           {
               if(iss.empty())
               return false;
               if(iss.top() == '(')
               {
                   iss.pop();
                   continue;
               }
               else
               iss.push(s[i]);
           }
           if(s[i] == ']')
           {
               if(iss.empty())
               return false;
               if(iss.top() == '[')
               {
                   iss.pop();
                   continue;
               }
               else
               iss.push(s[i]);
           }
            if(s[i] == '}')
           {
               if(iss.empty())
               return false;
               if(iss.top() == '{')
               {
                   iss.pop();
                   continue;
               }
               else
               iss.push(s[i]);
           }
        }
        if(iss.empty())
        return true;
        else
        return false;
    }
};

carl(只在栈内放入匹配的括号)

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

LeetCode1047

题目:

 自己解法:(持续入栈 有相同就出栈)

class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> iss;
        for(int i = 0;i <s.size();i++)
        {
            if(iss.empty())
            iss.push(s[i]);
            else if(iss.top() == s[i])
            {
              iss.pop(); 
              continue; 
            }
            else
            iss.push(s[i]);
        }
        string ss;
        while(!iss.empty())
        {
            ss.insert(ss.begin(),iss.top());
            iss.pop();
        }
        return ss;
    }
};

Carl解法:(直接把字符串当做栈)

class Solution {
public:
    string removeDuplicates(string S) {
        string result;
        for(char s : S) {
            if(result.empty() || result.back() != s) {
                result.push_back(s);
            }
            else {
                result.pop_back();
            }
        }
        return result;
    }
};

LeetCode150

题目:

解法:(注意取字符串里的整数时需要用stoll)

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<long long> con;
        for(int i = 0;i < tokens.size();i++)
        {
            if(tokens[i] == "+")
            {
                int x = con.top();
                con.pop();
                int y = con.top();
                con.pop();
                int sum = x + y;
                con.push(sum);
            }
            else if(tokens[i] == "-")
            {
                int n = con.top();
                con.pop();
                int m = con.top();
                con.pop();
                int se = m - n;
                con.push(se);
            }
            else if(tokens[i] == "*")
            {
                int a = con.top();
                con.pop();
                int b = con.top();
                con.pop();
                int mu = a * b;
                con.push(mu);
            }
            else if(tokens[i] == "/")
            {
                int d = con.top();
                con.pop();
                int c = con.top();
                con.pop();
                int ce = c / d;
                con.push(ce);
            }
            else
            con.push(stoll(tokens[i]));
        }
        return con.top();
    }
};

 字符串转换

  • stoi() 将字符串转换为整型
  • stoll() 将字符串转换为long long
  • stof() 将字符串转换为float型
  • stod() 将字符串转换为double型

感悟:

栈的用法还是需要好好熟记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值