LeetCode热题100刷题12:20. 有效的括号、394. 字符串解码、739. 每日温度、155. 最小栈、139. 单词拆分

20. 有效的括号

在这里插入图片描述

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

394. 字符串解码

在这里插入图片描述

class Solution {
public:
    string getDigits(string &s, size_t &ptr) {
        string ret = "";
        while(isdigit(s[ptr])) {
            ret.push_back(s[ptr++]);
        }
        return ret;
    }
    string getString(vector<string>& v){
        string ret;
        for(const auto &s : v) {
            ret+=s;
        }
        return ret;
    }
    string decodeString(string s) {
        vector<string> stk;
        size_t ptr = 0;
        while(ptr < s.size()) {
            char cur = s[ptr];
            if(isdigit(cur)) {
                string digits = getDigits(s,ptr);
                stk.push_back(digits);
            }else if(isalpha(cur) || cur=='[') {
                stk.push_back(string(1,s[ptr++]));
            }else{
                ++ptr;
                vector<string> sub;
                while(stk.back()!="[") {
                    sub.push_back(stk.back());
                    stk.pop_back();
                }
                reverse(sub.begin(),sub.end());

                stk.pop_back();
                int repTime = stoi(stk.back());
                stk.pop_back();
                string t, o = getString(sub);

                while(repTime--)
                    t+=o;
                stk.push_back(t);
            }
        }
        return getString(stk);
    }
};

739. 每日温度

在这里插入图片描述

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        if(temperatures.size()==1)
            return vector<int>(0);
        stack<int> st;
        vector<int> res(temperatures.size(),0);
        st.push(0);
        for(int i=0;i<temperatures.size();i++) {
            if(temperatures[i] <= temperatures[st.top()]) {
                st.push(i);
            }
            else {
                while(!st.empty() && temperatures[i] > temperatures[st.top()]) {
                    res[st.top()] = i-st.top();
                    st.pop();
                }
                st.push(i);
            }
        }
        return res;
    }
};

155. 最小栈

加粗借助一个栈min_st保存加入栈顶元素后当前栈的最小值

class MinStack {
    stack<int> x_st;
    stack<int> min_st; //辅助栈,每一个元素入栈的时候都附带存储一个当前站内元素的最小值
public:
    MinStack() {
        min_st.push(INT_MAX);
    }
    
    void push(int val) {
        x_st.push(val);
        min_st.push(min(min_st.top(),val));
    }
    
    void pop() {
        x_st.pop();
        min_st.pop();
    }
    
    int top() {
        return x_st.top();
    }
    
    int getMin() {
        return min_st.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

139. 单词拆分

动态规划的题
在这里插入图片描述

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordset(wordDict.begin(),wordDict.end());

        vector<bool> dp(s.size()+1,false);
        dp[0]=true;
        for(int i=0;i<=s.size();i++) {
            for(int j=0;j<i;j++) {
                string str = s.substr(j,i-j);
                if(wordset.find(str) != wordset.end() && dp[j])
                    dp[i]=true;
            }
        }
        return dp[s.size()];
    }
};

相关推荐

  1. LeetCode100394. 字符串解码

    2024-07-13 13:40:02       49 阅读
  2. LeetCode100】20. 有效括号

    2024-07-13 13:40:02       42 阅读
  3. LeetCode100】【每日温度

    2024-07-13 13:40:02       35 阅读
  4. LeetCode100155.

    2024-07-13 13:40:02       36 阅读
  5. LeetCode-100155.

    2024-07-13 13:40:02       37 阅读
  6. LeetCode100】【动态规划】单词

    2024-07-13 13:40:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 13:40:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 13:40:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 13:40:02       58 阅读
  4. Python语言-面向对象

    2024-07-13 13:40:02       69 阅读

热门阅读

  1. vue-侦听器

    2024-07-13 13:40:02       23 阅读
  2. Dubbo 核心概念介绍

    2024-07-13 13:40:02       20 阅读
  3. springboot的mybatis使用CONCAT模糊查询

    2024-07-13 13:40:02       20 阅读
  4. std::filesystem::current_path().generic_string()的bug

    2024-07-13 13:40:02       23 阅读
  5. 【Android】在渲染生效前提前测量View大小

    2024-07-13 13:40:02       22 阅读
  6. 基于节点嵌入的链接预测(暂时这样吧)

    2024-07-13 13:40:02       19 阅读