LeetCode 22. Generate Parentheses(构造)
题目大意:给n个'(' 和 ')',构造出所有的长度为2*n并且有效的(可匹配的)字符串。
题目分析:这道题不难,可以直接搜索出所有可能的字符串,然后再逐一判断是否合法即可。但是还有更好的办法,实际上,“判断是否合法”这一操作是冗余的,如果可以直接朝着满足可匹配性的方向进行构造,就可避免这一冗余操作,这需要换一个角度思考。
一个有效(可匹配)字符串中, '(' 的个数决定了 ')' 的个数(从左向右看)。所以,初始时,可以看成是 “有n个 '(' 还没有用、有0个 ')' 必须要用”。这样,在搜索的时候就能避开无效(不可匹配)的情况。
代码如下:
class Solution {
private:
void dfs(vector<string>& s,string p,int n,int m){///已经构造好的字符串为p,还剩n个'('可用,并且必须要用m个')'。
if(n==0&&m==0){
s.push_back(p);
return ;
}
if(m>0) dfs(s,p+')',n,m-1);
if(n>0) dfs(s,p+'(',n-1,m+1);
}
public:
vector<string> generateParenthesis(int n) {
vector<string>s;
dfs(s,"",n,0);
return s;
}
};
LeetCode 22. Generate Parentheses(构造)的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- LeetCode 22. Generate Parentheses
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生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [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 ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
随机推荐
- mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...
- 有趣的js匿名函数写法(function嵌套)
例子没有什么实际意义,只能做为思路参考 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- (六)最最基本的git操作
1.git init——初始化仓库 初始化成功的标志如下(.git默认为隐藏) 2.git status——查看仓库状态 ps:未跟踪的文件 (untracked files) 不妨在尝试在仓库建立一 ...
- Antlr4 SQL Query 解析实例
grammar MysqlQuery; @header{package com.antlr.mysql.query;} AS : A S; SELECT : S E L E C T; FROM : F ...
- vc++之stdafx.h
关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...
- 20145333茹翔 Exp7 网络欺诈技术防范
20145333茹翔 Exp7 网络欺诈技术防范 1.实验后回答问题 (1)通常在什么场景下容易受到DNS spoof攻击 局域网内的攻击,arp入侵攻击和DNS欺骗攻击 公共wifi点上的攻击. ( ...
- 小工具:使用Python自动生成MD风格链接
很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护.最近发现自己刷了不少LC的题目了,想搬运到这个仓库上. 玩Github最重要的当然是写README了,MD的逼格决定了项目牛 ...
- JS(JavaScript)脚本库的积累
在现在互联网盛行的时代,使得B/S架构飞速发展.曾经在大学的时候我一直都梦想着毕业后要找一个像腾讯这样大企业做C/S方面的开发工作(其实现在腾讯也有很多B/S软件),因为C/S体验度非常高,感觉非常好 ...
- Python3基础 函数 有参数有返回值 对传入的参数加1
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 在ubuntu16.04上搭建视频服务器
推荐方案三:超级简单 方案一.hls (缺陷:需要花很多时间切片) 1.Distributor ID: Ubuntu Description: Ubuntu 16.04.3 LTS Release ...