博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode #22 Generate Parentheses
阅读量:4462 次
发布时间:2019-06-08

本文共 900 字,大约阅读时间需要 3 分钟。

LeetCode #22 Generate Parentheses

Question

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:

[  "((()))",  "(()())",  "(())()",  "()(())",  "()()()"]

Solution

Approach #1

class Solution {    func generateParenthesis(_ n: Int) -> [String] {        var results: [String] = []        func parenthesisStrings(_ str: String, _ open: Int, _ close: Int) {            if open == 0, close == 0 {                results.append(str)                return            }            if open > 0 {                parenthesisStrings(str + "(", open - 1, close)            }            if close > open {                parenthesisStrings(str + ")", open, close - 1)            }        }        parenthesisStrings("", n, n)        return results    }}

转载请注明出处:

转载于:https://www.cnblogs.com/silence-cnblogs/p/7065835.html

你可能感兴趣的文章
拜师鸟哥之linux学习体会(13)——linux账号管理与ACL权限设定
查看>>
Shell编程-条件测试 | 基础篇
查看>>
[Spring Boot Reference Guide] 读书笔记一 Getting Started
查看>>
AngularJs学习笔记1——总体介绍
查看>>
C语言第十二讲,文件操作.
查看>>
绝对定位和相对定位
查看>>
处女座的测验(一)
查看>>
实习第二天——学习mac终端命令(unix命令)和git代码管理
查看>>
初识redis
查看>>
微信支付
查看>>
2018、
查看>>
iOS开发拓展篇—CoreLocation定位服务
查看>>
吴裕雄--天生自然 高等数学学习:含参变量的积分
查看>>
ServletContext对象的使用
查看>>
Python-aiohttp百万并发
查看>>
leetcode--Permutation Sequence
查看>>
[转载] 腾讯开源的rapidjson
查看>>
关于Oracle数据库字符集
查看>>
Cookie Session 与Token
查看>>
[POJ1655]Balancing Act
查看>>