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 + ')');
     
}
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章