[LeetCode]22. Generate Parentheses
2016-04-07 15:48
441 查看
Problem Description
[]https://leetcode.com/problems/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:
“((()))”, “(()())”, “(())()”, “()(())”, “()()()”
思路
递归Code
package q022; import java.util.ArrayList; import java.util.List; public class Solution { public List<String> generateParenthesis(int n) { List<String> ans = new ArrayList<String>(); helper(ans, "", n, n); return ans; } public void helper(List<String> ans, String tmp, int n, int m) { if (n == 0 && m == 0) ans.add(new String(tmp) 4000 ); if (n >= 1) helper(ans, tmp + "(", n - 1, m); if (m >= 1 && n < m) helper(ans, tmp + ")", n, m - 1); } }
相关文章推荐
- linux下配置JDK JAVA环境
- Java序列化(Serializable)与反序列化
- Android SoundPool播放实例和方法详解
- Android 开发之 定时器 Handler + Runnable方式
- iOS开发基础学习笔记——导航控制器2->导航栏(UINavigationBar)/工具栏(toolBar)/导航元素按钮(UINavigationItem)
- sqlserver查找包含关键词的视图
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 为 HTML 添加新元素(html5新功能)
- PHP语言开发Paypal支付demo的具体实现
- shell_sctipts: 删除mysql备份到最后7日
- 用PS制作漂亮的小气泡
- php对XML的解析
- Lintcode:买卖股票的最佳时机
- cf#312-E-A Simple Task- 线段树+暴力(计数排序)
- 升级JDK版本后,SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializ
- Android手机在开发调试时logcat不显示输出信息的解决办法
- iOS项目中安装和使用 Cocoapods
- 网络原理,以及对VMware Workstation虚拟网络VMnet0、VMnet1、VMnet8的图解
- Windows下react-native安装步骤以及安装时候遇到各类已填平的坑
- NSTimer扩展block版(用block写定时器)