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

问题解析:

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

链接:

思路标签

算法:栈结构

解答:

  • 利用栈是否为空或者右符号是否能和栈顶配对来进行判断。
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 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

链接:

思路标签

算法:递归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 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

链接:

思路标签

算法:递归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);
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值