题目描述: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:

"((()))", "(()())", "(())()", "()(())", "()()()"

分析:

递归

代码如下:

class Solution {
private:
vector<string> ret;
public:
void solve(int dep, int maxDep, int leftNum, int leftNumTotal, string s)
{
//如果(个数超出,return
if (leftNumTotal * 2 > maxDep)
return; //如果长度足够,return
if (dep == maxDep){
ret.push_back(s);
return;
} for(int i = 0; i < 2; i++) //加 (
if (i == 0)
solve(dep + 1, maxDep, leftNum + 1, leftNumTotal + 1, s + '(');
//加 )
else if (leftNum > 0)
solve(dep + 1, maxDep, leftNum - 1, leftNumTotal, s + ')');
} vector<string> generateParenthesis(int n){
ret.clear();
solve(0, 2 * n, 0, 0, "");
return ret;
}
};

Java:

    public List<String> generateParenthesis(int n) {

        ArrayList<String> result = new ArrayList<String>();
dfs(result, "", n, n);
return result; } public void dfs(ArrayList<String> result, String s, int left, int right) { if (left > right) {
return;
} if (left == 0 && right == 0) {
result.add(s);
return;
} if (left > 0) {
dfs(result, s + "(", left - 1, right);
} if (right > 0) {
dfs(result, s + ")", left, right - 1);
} }

LeetCode 022 Generate Parentheses的更多相关文章

  1. 【JAVA、C++】LeetCode 022 Generate Parentheses

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

  2. 【题解】【排列组合】【回溯】【Leetcode】Generate Parentheses

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

  3. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  4. [LeetCode] 22. Generate Parentheses 生成括号

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

  5. LeetCode 22. Generate Parentheses

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

  6. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  7. 【leetcode】 Generate Parentheses (middle)☆

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

  8. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  9. leetcode之 Generate Parentheses

    题目:http://oj.leetcode.com/problems/generate-parentheses/ 描述:给定一个非负整数n,生成n对括号的所有合法排列. 解答: 该问题解的个数就是卡特 ...

随机推荐

  1. SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂)

    SpringBoot第五集:整合监听器/过滤器和拦截器(2020最新最易懂) 在实际开发过程中,经常会碰见一些比如系统启动初始化信息.统计在线人数.在线用户数.过滤敏/高词汇.访问权限控制(URL级别 ...

  2. [Luogu P2824] [HEOI2016/TJOI2016]排序 (线段树+二分答案)

    题面 传送门:https://www.luogu.org/problemnew/show/P2824 Solution 这题极其巧妙. 首先,如果直接做m次排序,显然会T得起飞. 注意一点:我们只需要 ...

  3. vue3.0-如何切换路由-路由模式ts

    如何更换路由模式 vue3版本如何切换路由模式?(注:更改后要重启下项目才能看到效果.) 博主,我搜了大半天的,你是怎么找到的? 如下图可看到

  4. Spring Boot API 统一返回格式封装

    今天给大家带来的是Spring Boot API 统一返回格式封装,我们在做项目的时候API 接口返回是需要统一格式的,只有这样前端的同学才可对接口返回的数据做统一处理,也可以使前后端分离 模式的开发 ...

  5. 如何理解直播APP源码开发中的音视频同步

    视频 直播APP源码的视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程,就像在一个本子的每一页画上画,然后快速翻动的感觉. 但是在实际应用中,并不是每一帧都是完整的画面,因为如果直 ...

  6. jQuery JSONP

    同源策略 浏览器不允许跨域发送Ajax请求,如你在http://127.0.0.1:8000/下发送一个Ajax请求去获取http://127.0.0.1:9000/的资源. 如何解决这个问题?其实j ...

  7. C语言I博课作业04

    这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2020-1/homework/11489 我在这个作业课程 ...

  8. 掉电后osdmap丢失无法启动osd的解决方案

    前言 本篇讲述的是一个比较极端的故障的恢复场景,在整个集群全部服务器突然掉电的时候,osd里面的osdmap可能会出现没刷到磁盘上的情况,这个时候osdmap的最新版本为空或者为没有这个文件 还有一种 ...

  9. PLSQL-解析XML

    DECLARE v_xmlclob CLOB := '<?xml version="1.0" encoding="UTF-8"?> <head ...

  10. [LeetCode题解]19. 删除链表的倒数第N个节点 | 双指针 + 一次遍历

    解题思路 双指针:第一个指针先走 n 步,然后两个指针同时走. 这里要注意当链表长度<=n,要删除头节点. 代码 /** * Definition for singly-linked list. ...