leetcode个人题解——#22 Generate Parentheses
思路:
递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号。
class Solution {
public:
vector<string> ans;
int num;
void brackets(string res, int left, int right, int sum)
{
if(sum == * num) {
ans.push_back(res);
}
else{
if(left < right || left < num)brackets(res + '(', left + , right, sum + );
if(left > right)brackets(res + ')', left, right + , sum + );
}
}
vector<string> generateParenthesis(int n) {
if(n == ) return ans;
num = n;
brackets("", , , );
return ans;
}
};
leetcode个人题解——#22 Generate Parentheses的更多相关文章
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题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 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 【LeetCode】22. Generate Parentheses (I thought I know Python...)
I thought I know Python... Actually , I know nothing... 这个题真想让人背下来啊,每一句都很帅!!! Given n pairs of paren ...
随机推荐
- mysql千万级数据库插入速度和读取速度的调整
mysql上百万数据读取和插入更新一般没什么问题,但上千万后速度会很慢,如何调整配置,提高效率.如下: 1.尽量将数据一次性写入DataFile和减少数据库的checkpoint操作,调整如下参数: ...
- linux配置mysq与navicat关联
第一步:在linux中安装mysql(执行如下语句) 安装 mysql: yum install mysql yum install mysql-server yum install mysql-de ...
- Codeforces Round #483 (Div. 2)C题
C. Finite or not? time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 利用mysqlbinlog_flashback闪回丢失数据
today,i'll have a test with the open source tool mysqlbinlog_flashback which is released by ...
- 针对jquery的ajax中的参数理解
1. url 发送请求的地址.为空表示当前页. $.ajax({ type: "post", data: studentInfo, contentType: "appli ...
- 路由(二) router-link的使用
main.js import Vue from 'vue'import App from './App'import VueRouter from 'vue-router'import footer ...
- consonant_摩擦音
consonant_摩擦音_[t∫].[dʒ].[tr].[dr].[ts].[dz] 破擦音:即有爆破音又有摩擦音. [t∫]:噘嘴,舌尖抵住上牙龈,舌头下切,用一瞬间的气流发出声音,不震动. ch ...
- python2x和python3x的一些区别
python2x:各种按照自己代码习惯给python贡献源码 python3x:重写之后的源码,优美,清晰,简单 版本 打印函数 rang函数 输入函数 python2x print 或 print( ...
- ruby中的字符串分隔符--split
当字符串是以“:”隔开时,可以这样写: column = str.split(/:/) 这样,column就是字符串每栏的值所构成的数组. eg: str = "Ruby in a shel ...
- 检测微信小程序是否被反编译获取源码
众所周知,微信小程序的代码安全性很弱,很容易被别人反编译获取源码.我自己的小程序也被别人反编译拿到源码还上线了,非常无语. 既然客户端不好防范,服务端还是可以做点手脚的. 小程序的Referer是不可 ...