描述

Given n pairs of parentheses, write a function to generate all combinations of wellformed parentheses.
For example, given n = 3, a solution set is:
"((()))", "(()())", "(())()", "()(())", "()()()"

思路

该题目有一个规则是左括号个数得小于右括号个数,根据这个规则,可用通过动态规划来解决这个题目

代码

package com.lilei.myes.es.pack1107;public class generate_parentheses {public static void main(String[] args) {int num = 4;genp("", num, num);}public static void genp(String s, int left, int right) {if (left > 0 && right > 0) {if (left == right) {genp(s + "(", left - 1, right);} else if (left < right) {genp(s + "(", left - 1, right);genp(s + ")", left, right - 1);}} else {for (int i = 0; i < right; i++)s = s + ")";System.out.println(s);}}}

  

转载于:https://www.cnblogs.com/lilei2blog/p/7799586.html

Generate Parentheses相关推荐

  1. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  2. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  3. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  4. LeetCode Generate Parentheses

    原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...

  5. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

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

  6. 【LeetCode从零单排】No22.Generate Parentheses

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

  7. 22. Generate Parentheses

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

  8. [LeetCode] Generate Parentheses

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

  9. [LeetCode] #22 Generate Parentheses

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

最新文章

  1. LINUX驱动注册过程失败处理不当引起的恶果
  2. 【转】排除被冲销的物料凭证
  3. 以用户体验为导向的设计表现
  4. 直播实录 | 哈工大博士生周青宇:从编码器与解码器端改进生成式句子摘要
  5. boost::fusion::unfused_typed用法的测试程序
  6. 细数家庭安防五大乱象 何时能步入正轨
  7. python 三元运算、列表推倒式、字典推倒式、生成器生成式
  8. 51nod 博弈论水题
  9. mysql数据库p_PbootCMS Sqlite数据库转Mysql数据库教程
  10. 基于proteus的一个微程序CPU
  11. 程序人生 | 文艺程序员使用代码发展诗歌
  12. 爬虫 爬取百思不得姐网站
  13. 即时通讯(WebSocket、Socket IO)
  14. 二阶系统响应指标图_二阶系统的脉冲响应.ppt
  15. eap wifi 证书_如何以编程方式在Android中安装CA证书(用于EAP WiFi配置)?
  16. 【C++实现】编译原理 免考小队 NFA转换为等价的DFA
  17. 美国军方又出黑科技,实现用意念控制无人机
  18. linux机器上crontab定时任务将日志输出到以日期命名的log文件
  19. sizeof c语言是什么表达式,C 语言 sizeof() 运算符
  20. JS练习之鼠标经过切换图片

热门文章

  1. 美团外卖骑手背后的AI技术
  2. redhat7配置本地源
  3. threadlocal使用_Java多线程数据共享神器ThreadLocal
  4. PHP 模拟真实ip,PHP实现-获取用户的真实IP
  5. anaconda3安装_Anaconda3软件与安装教程
  6. mysql如何定义消耗资源多的sql语句_如何查找消耗资源较大的SQL
  7. hust1346(两个线段的最近距离和最小距离)
  8. php.ini文件可以复制吗,php安装完成以后要复制php.ini文件
  9. 2021-03-01 英文写作中的“许多”
  10. Webshell免杀绕过waf