LeetCode-20、22:Valid、Generate Parentheses(括号匹配、生成)

题目20:Valid Parentheses

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

例子:

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true

问题解析:

给定一个只包括 ‘(‘,’)’,’{‘,’}’,’[‘,’]’ 的字符串,判断字符串是否有效。

链接:

  • LeetCode:https://leetcode.com/problems/valid-parentheses/description/

思路标签

算法:栈结构

解答:

  • 利用栈是否为空或者右符号是否能和栈顶配对来进行判断。
class Solution {
public:
    bool isValid(string s) {
        stack<char> paren;
        for (char& c : s) {
            switch (c) {
                case '(': 
                case '{': 
                case '[': paren.push(c); break;
                case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
                case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break;
                case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break;
                default: ; // pass
            }
        }
        return paren.empty() ;
    }
};

题目22:Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

例子:

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

问题解析:

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

链接:

  • LeetCode:https://leetcode.com/problems/generate-parentheses/description/

思路标签

算法:递归DFS

解答:

  • 根据题目规则我们能够知道,符号只有两种“(”和“)”,我们分别记为其个数为左括号left和右括号right,所以满足条件的组合一定是left<=right。否则,不符合要求。
  • 同时,当left和right都为0时,当前组合满足条件。
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        generateParenthesisDFS(n, n, "", res);
        return res;
    }

    void generateParenthesisDFS(int left, int right, string out, vector<string> &res){
        if(left > right)
            return;
        if(left == 0 && right == 0)
            res.push_back(out);
        else{
            if(left > 0) generateParenthesisDFS(left-1, right, out+'(', res);
            if(right > 0) generateParenthesisDFS(left, right-1, out+')', res);
        }
    }
};

题目:Generate Parentheses

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

例子:

For example, given n = 3, a solution set is:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

问题解析:

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

链接:

  • LeetCode:https://leetcode.com/problems/generate-parentheses/description/

思路标签

算法:递归DFS

解答:

  • 根据题目规则我们能够知道,符号只有两种“(”和“)”,我们分别记为其个数为左括号left和右括号right,所以满足条件的组合一定是left<=right。否则,不符合要求。
  • 同时,当left和right都为0时,当前组合满足条件。
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        generateParenthesisDFS(n, n, "", res);
        return res;
    }

    void generateParenthesisDFS(int left, int right, string out, vector<string> &res){
        if(left > right)
            return;
        if(left == 0 && right == 0)
            res.push_back(out);
        else{
            if(left > 0) generateParenthesisDFS(left-1, right, out+'(', res);
            if(right > 0) generateParenthesisDFS(left, right-1, out+')', res);
        }
    }
};

你可能感兴趣的:(LeetCode,括号,栈,LeetCode刷题,LeetCode,刷题)