[leetcode]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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
题意: 给定一个长度,生成所有此长度的合法括号序列。
Solution1:Backtracking.
We can observe that two constraints:
(1) left Parentheses should come faster ahead to reach given n.
(2) in each level, we increment left Parentheses count (or right Parentheses count) from given n, and add '(' (or ')') into path
At last, left and right Parentheses count become 0, we can gerenate a result path.

code:
/*
Time: O(2^n).
Space: O(n). use O(n) space to store the sequence(path)
*/
class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
if (n == 0) return result;
helper("", n, n, result);
return result; } private void helper(String path, int left, int right, List<String> result) {
// corner
if (left > right) return;
// base
if (left == 0 && right == 0) {
result.add(path);
return;
}
if (left > 0) {
helper(path + "(", left - 1, right, result);
}
if (right > 0) {
helper(path + ")", left, right - 1, result);
}
}
}
[leetcode]22. Generate Parentheses生成括号的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] 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] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
随机推荐
- python中时间、日期、时间戳的转换
1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 # 字符类型的时间 tss1 ...
- Java_集合_ArrayLish Comparator比较排序 小笔记
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; class Teacher ...
- autotools
文章目录 原文地址 Autotools上手指南1--autoconf基本思想 Autotools上手指南2--autoscan生成configure.ac Autotools上手指南3--autohe ...
- 聊聊Java反射
反射是Java最重要的特性.通过Java反射可以在运行时知道一个类的所有成员和方法,知道一个对象的类类型.成员和方法的所有信息,进而调用对象的方法或生成对象的代理或包装类. Java是面向对象语言,除 ...
- javaScript 中的私有,共有,特权属性和方法
function constructor () { var private_v; // 私有属性 var private_f = function () { // 私有方法 // code }; th ...
- c++ CreateProcess调用dos命令
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h> #include &l ...
- LeetCode——727.Minimum Window Subsequence
一.题目链接:https://leetcode.com/problems/minimum-window-substring/ 二.题目大意: 给定两个字符串S和T,要求从S中找出包含T中所有字母的最短 ...
- 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器
第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...
- 常量&字符编码
day1 name='Nod Chen' name2=name print('My name is ',name,name2) name='Luna zhou' print(name,name2) _ ...
- rabbitMQ windows 安装 入门
转: https://www.cnblogs.com/junrong624/p/4121656.html 这里需要下载 rabbitmq, 我网盘里有今天没时间上传了,下次吧 1.下载,其实erlan ...