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表示n对括号,就有n个左括号和n个右括号,我们定义两个变量left和right分别表示字符串中左括号的个数和右括号的个数。如果字符串中右括号的个数大于左括号的个数,这就会出现)(的情况,所以不作处理,直接返回。当左括号的个数小于n时,这个时候就可以添加左括号,右括号个数小于左括号时,可以添加右括号。也就是说,两种情况都可以,所以都进行尝试。当字符串长度和n*2相等时就可以添加到List中了。
class Solution {
public List<String> generateParenthesis(int n) {
/*
*/
List<String> list=new ArrayList<>();
helper(list,"",0,0,n);
return list;
}
/*
求n时的括号组合。
left表示此时字符串中左括号的个数,right表示字符串中右括号的个数,
*/
public void helper(List<String> list,String str,int left,int right,int n){
if(str.length()==n*2){
list.add(str);
return ;
}
//当left小于n时,表示还可以添加左括号,当left大于right时,表示可以添加右括号
//注意:该字符串中,左括号的个数不可能小于右括号的个数,因为这样就是无效的输出。
if(left<n)
helper(list,str+"(",left+1,right,n);
if(left>right)
helper(list,str+")",left,right+1,n);
}
}
generate parentheses(生成括号)的更多相关文章
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode OJ:Generate Parentheses(括号生成)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
随机推荐
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...
- windows与linux的文件夹共享
公司配备了一台性能还算不错的电脑,不过是台式机.我在上面装了ubuntu,但是我的代码工作目录全部都在我自己的win7笔记本上.有时程序开多了就容易卡,于是想到用装ubuntu的台式机来访问我win7 ...
- 使用LRU算法缓存图片,android 3.0
在您的UI中显示单个图片是非常简单的,如果您需要一次显示很多图片就有点复杂了.在很多情况下 (例如使用 ListView, GridView 或者 ViewPager控件), 显示在屏幕上的图片以及即 ...
- Swift基础之Swift调用OC语言文件使用步骤
Swift语言中,有很多封装类并没有,如果需要使用到,就需要桥接OC语言中的类,这时候就需要使用桥接头文件,一下是使用的步骤: 创建一个Swift项目Demo,然后新建一个OC语言的文件 如图: 创建 ...
- 将Ext JS 6应用程序导入Web项目
由于Ext JS 6包含了Sencha Touch,因而在应用程序结构有了些改变,Ext JS 5的方法已经不适用于新版本了.经过研究,发现6导入Web项目要比5简单. 下面来说说导入的过程. 使用S ...
- C++ Primer 有感(标准库pair)
与关联容器相关的模板类型,包含两个数据成员,在utility头文件中定义. pair类型提供的操作: pair<T1,T2> p1; pair<T1,T2> p1(v1,v2) ...
- Linux IPC实践(6) --System V消息队列(3)
消息队列综合案例 消息队列实现回射客户/服务器 server进程接收时, 指定msgtyp为0, 从队首不断接收消息 server进程发送时, 将mtype指定为接收到的client进程的pid ...
- Leetcode_36_Valid Sudoku
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42528601 Determine if a Sudoku ...
- 1、libgdx简介
Libgdx 是一个跨平台和可视化的的开发框架.它当前支持Windows,Linux,Mac OS X,Android,IOS和HTML5作为目标平台. Libgdx允许你一次编写代码不经修改部署到多 ...
- Android 数据库框架ormlite
Android 数据库框架ormlite 使用精要 前言 本篇博客记录一下笔者在实际开发中使用到的一个数据库框架,这个可以让我们快速实现数据库操作,避免频繁手写sql,提高我们的开发效率,减少出错的机 ...