22. Generate Parentheses(ML)
22. Generate Parentheses
. Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = , a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]
class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ if n == 0: return [''] ans = [] for c in xrange(n): for left in self.generateParenthesis(c): for right in self.generateParenthesis(n-1-c): ans.append('({}){}'.format(left, right)) return ans
class Solution(object): # Brute Force def generateParenthesis(self, n): def generate(A=[]): if len(A) == 2 * n: if valid(A): ans.append("".join(A)) else: A.append('(') generate(A) A.pop() A.append(')') generate(A) A.pop() def valid(A): bal = 0 for c in A: if c == '(': bal += 1 else: bal -= 1 if bal < 0: return False return bal == 0 ans = [] generate() return ans # Backtracking def generateParenthesis2(self, N): ans = [] def backtrack(S='', left=0, right=0): if len(S) == 2 * N: ans.append(S) return if left < N: backtrack(S + '(', left + 1, right) if right < left: backtrack(S + ')', left, right + 1) backtrack() return ans # Closure Number def generateParenthesis3(self, N): if N == 0: return [''] ans = [] for c in range(N): # for c in xrange(N): for left in self.generateParenthesis(c): for right in self.generateParenthesis(N - 1 - c): ans.append('({}){}'.format(left, right)) return ans sn = Solution() print(sn.generateParenthesis(3)) print(sn.generateParenthesis2(3)) print(sn.generateParenthesis3(3))
22. Generate Parentheses(ML)的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 22. Generate Parentheses (recursion algorithm)
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 ...
- 22. Generate Parentheses——本质:树,DFS求解可能的path
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
随机推荐
- python之if使用方法举例
if使用方法举例: import random #随机生成1-100的整数 n = random.randint(1, 100) if n > 50: print(n, "> 5 ...
- How to split DMG on macOS
hdiutil segment /users/test/test1.dmg -segmentsize 4000m -o /users/test/test2.dmg
- 使用 Travis CI 自动部署 Hexo 站点至 GitHub Pages
Hexo 与 GitHub Pages 安装配置请参考:Hexo 与 GitHub Pages 本文源码与生成的静态文件在同一项目下,源码在 source 分支,静态文件在 master 分支 新增 ...
- axis函数
axis函数 axis([xmin xmax ymin ymax]) 用来标注输出的图线的最大值最小值. MATLAB中坐标系的设置函数 MATLAB 函数 axis([XMIN XMAX YMI ...
- User Authentication with Angular and ASP.NET Core
User authentication is a fundamental part of any meaningful application. Unfortunately, implementing ...
- bzoj2152-[国家集训队]聪聪可可
Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一般情况下石头剪刀布就好 ...
- Mysql(Mariadb)数据库主从复制
Mysql主从复制的实现原理图大致如下: MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据 ...
- memcached安装报错 error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory解决
我是从其他服务器scp来的memcached(~~~整个文件夹的那种,windows用多了的后遗症) 在准备运行 ./memcached -d -u root -l localhost -m 800 ...
- Luogu5205 【模板】多项式开根(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! 写NTT把i<<=1写成了i<<=2,又调了一年.发现我的日常就是数组开 ...
- 三:C#对象转换Json时的一些高级(特殊)设置;
导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型: 二:C#对象.集合.DataTable与Json内容互转示例: ...