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 ...
随机推荐
- 由Oracle 11g SYSAUX 和 SYSTEM 表空间回收引发的联想
0x00--目的 整理一下以前一个SYSTEM表空间和SYSAUX表空间使用率达到99%上限的处理思路和相关知识点,好记性不如烂笔头 0x01--表空间使用率现状 通过查询可得知目前表空间使用情况如下 ...
- AtomicStampedReference解决ABA问题
在运用CAS做Lock-Free操作中有一个经典的ABA问题: 线程1准备用CAS将变量的值由A替换为B,在此之前,线程2将变量的值由A替换为C,又由C替换为A,然后线程1执行CAS时发现变量的值 ...
- hdu Hat's Fibonacci(用了kuangbin模板)
大数的位数设置很坑,设成700会越界,设成800会超空间,最后设成了750居然就过了.... #include <iostream> #include <cstdio> #in ...
- 通过xshell在linux上安装mysql5.7(终极版)
通过xshell在linux上安装mysql5.7(终极版) 0)通过xshell连接到远程服务器 1)彻底删除原来安装的mysql 首先查看:rpm -qa|grep -i mysql 删除操作(一 ...
- linux系统基础之-----磁盘结构(基于centos7.4 1708)
- 前端String转json
1.data = eval("("+data+")");2.JSON.parse(data);
- Git命令中日常不注意又很重要的坑
引言 简单聊一下Git的常用命令和概念,其中很多命令开发者在使用时用法不当导致出现很多问题: 比如,新创建的分支没有追踪想要追踪的分支,很想看到版本提交的内容 以下是觉得比较好用并且完整的 ...
- Oracle之单表查询及常用函数
1.语法: select 字段列表 from 表名 [where 查询条件] [group by 分组] [having 分组条件] [order by 排序] select * 代表查询所有的字段 ...
- 『Linux基础 - 5 』Linux常用命令(2)
这篇笔记的只要知识点: (1)ls查看文件信息,列表中每个字符所代表的含义 (2) 使用通配符匹配文件 (3) chmod命令:修改文件或目录权限 (4) 与用户相关命令(who.su.exit.pa ...
- Quartus II 项目文件分类及内容