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

20. 有效的括号

cpp 复制代码
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. 字符串解码

cpp 复制代码
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. 每日温度

cpp 复制代码
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保存加入栈顶元素后当前栈的最小值

cpp 复制代码
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. 单词拆分

动态规划的题

cpp 复制代码
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()];
    }
};
相关推荐
初学小刘8 分钟前
机器学习中的聚类与集成算法:从基础到应用
算法·机器学习·聚类
杜子不疼.36 分钟前
【LeetCode 415】—字符串相加算法详解
算法·leetcode·职场和发展
仙俊红42 分钟前
LeetCode每日一题,2025-08-21
算法·leetcode·职场和发展
楽码1 小时前
傻傻分不清:信息和通信复杂度
网络协议·算法·函数式编程
凳子(刘博浩)1 小时前
机器学习两大核心算法:集成学习与 K-Means 聚类详解
算法·机器学习·集成学习
重启的码农1 小时前
llama.cpp 分布式推理介绍(5) RPC 通信协议
c++·人工智能·神经网络
liulilittle1 小时前
UTF-8 编解码可视化分析
c++·字符串·unicode·string·字符·char·utf8
重启的码农1 小时前
llama.cpp 分布式推理介绍(6) 张量序列化 (rpc_tensor)
c++·人工智能·神经网络
已读不回1432 小时前
设计模式-工厂模式
前端·算法·代码规范
CoovallyAIHub2 小时前
YOLOv8-SMOT:基于切片辅助训练与自适应运动关联的无人机视角小目标实时追踪框架
深度学习·算法·计算机视觉