Leetcode——用栈判断有效括号总结

valid-parentheses

题目链接:https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2?tpId=46&tqId=29158&tPage=7&rp=7&ru=%2Fta%2Fleetcode&qru=%2Fta%2Fleetcode%2Fquestion-ranking

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

longest-valid-parentheses

题目链接:https://www.nowcoder.com/practice/45fd68024a4c4e97a8d6c45fc61dc6ad?tpId=46&tqId=29147&tPage=6&rp=6&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking

class Solution {
public:
    int longestValidParentheses(string s) {
          int ans=0;
        stack<int> st;
        st.push(-1);
        for(int i=0;i<s.size();i++){
            if(s[i]=='(') st.push(i);
            else{
                 st.pop();
                 if(!st.size()) 
                     st.push(i);
                else
                  ans=max(ans,i-st.top());
                 
                 
            }
        }
        return ans;
    }
};
public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push('(');
            } else if (!stack.empty() && stack.peek() == '(') {
                stack.pop();
            } else {
                return false;
            }
        }
        return stack.empty();
    }
    public int longestValidParentheses(String s) {
        int maxlen = 0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 2; j <= s.length(); j+=2) {
                if (isValid(s.substring(i, j))) {
                    maxlen = Math.max(maxlen, j - i);
                }
            }
        }
        return maxlen;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode/

3 generate-parentheses

题目链接:https://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca?tpId=46&tqId=29157&tPage=7&rp=7&ru=%2Fta%2Fleetcode&qru=%2Fta%2Fleetcode%2Fquestion-ranking

//链接:https://www.nowcoder.com/questionTerminal/c9addb265cdf4cdd92c092c655d164ca
//left代表左括号剩余个数,right同理
//1.每部递归注意递归体即有几种可选的操作  +'('  or + ')'
//2.考虑操作的限制条件,加右括号时满足左括号的剩余个数<右括号
//3.递归退出条件,左右括号个数都用光即可
class Solution {
public:
    vector<string> generateParenthesis(int n) {
 vector<string> res;
    if(n < 1) return res;
    int left = n, right = n;
    generateParenthesisDfs(res, left, right, "");
    return res;}
    void generateParenthesisDfs(vector<string> &res, int left, int right, string cur){
 //3.跳出条件
    
    if(left == 0 && right == 0){
        res.push_back(cur);
        return;//没有循环直接执行。执行完毕后,推出
    }
     
    //1.choice
    if(left > 0) generateParenthesisDfs(res, left - 1, right, cur + '(');
     
    //2.constrain
    if(right > 0 && right > left) generateParenthesisDfs(res, left, right - 1, cur + ')');
     
}
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值