72. Generate Parentheses && Valid Parentheses
Generate Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not。
思路:可用于卡特兰数一类题目。
- void getParenthesis(vector<string> &vec, string s, int left, int right) {
- if(!right && !left) { vec.push_back(s); return; }
- if(left > 0)
- getParenthesis(vec, s+"(", left-1, right);
- if(right > 0 && left < right)
- getParenthesis(vec, s+")", left, right-1);
- }
- class Solution {
- public:
- vector<string> generateParenthesis(int n) {
- vector<string> vec;
- getParenthesis(vec, string(), n, n);
- return vec;
- }
- };
Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
思路: 栈。对 S 的每个字符检查栈尾,若成对,则出栈,否则,入栈。
- class Solution {
- public:
- bool isValid(string s) {
- bool ans = true;
- char ch[6] = {'(', '{', '[', ']', '}', ')'};
- int hash[256] = {0};
- for(int i = 0; i < 6; ++i) hash[ch[i]] = i;
- string s2;
- for(size_t i = 0; i < s.size(); ++i) {
- if(s2 != "" && hash[s2.back()] + hash[s[i]] == 5) s2.pop_back();
- else s2.push_back(s[i]);
- }
- if(s2 != "") ans = false;
- return ans;
- }
- };
72. Generate Parentheses && Valid Parentheses的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- lettcode笔记--Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- QT5学习过程的小问题集锦
*** only available with -std=c++11 or -std=gnu++11 添加以下代码到*.pro文件. CONFIG += c++11 在 Qt creator 中设置 ...
- asd
弹出输入提示框 :window.prompt(); 24. 指定当前显示链接的位置 :window.location.href="URL" 25. 取出窗体中的所有表单的数量 :d ...
- 转载《android:scaleType属性》
在网上查了好多资料,大致都雷同,大家都是互相抄袭的,看着很费劲,不好理解,自己总结一下,留着需要看的话来查找. 代码中的例子如下: <ImageView android:id="@+i ...
- Python通用序列操作
1.序列概览 1.数据结构 序列.容器 Python中最基本的数据结构是序列,其有索引(从左到右第一个索引为0,从右到左第一个索引为-1). Python包含6中内建的序列: 列表 元组 字符串 Un ...
- Beta版本测试报告
爬虫测试: 由于爬虫是整个系统的数据来源,十分的重要,但是由于引用了jar包并且运行复杂,这里主要做功能性测试,通过增加seed,运行爬虫,可以在后台控制台看到日志的不断刷新以及数据库条目的不断增加, ...
- 【我是老中医】VMware在win8.1下开Ubuntu提示”内部错误"解决方案
这个题目起得很洋气啊,其实问题也比较好解决,但是我想多码几个字!!! 友情提示:如果不想看废话,请直接看最后的红字! 好的,咱们从头说(废话)起.话说我们学院每年都会组织大三的进行校企联合实训(其实就 ...
- 免费SSL-HTTS 申请与配置 NGINX配置
Let's Encrypt是很火的一个免费SSL证书发行项目,自动化发行证书,证书有90天的有效期.适合个人使用或者临时使用,不用再忍受自签发证书不受浏览器信赖的提示.Let's Encrypt已经发 ...
- 简述jsp之EL表达式和jstl及其使用
Jsp的指令之include指令include指令:代表的是页面的包含. 作用:可以把一些jsp的页面包含在一起,对外展示. 页面的布局,现在已经不用了,现在都用css+div进行布局.include ...
- IOS开发之--NSPredicate
我把常用的NSPredicate使用场景整理了一下 官方参考: https://developer.apple.com/library/mac/#documentation/Cocoa/Refer ...
- user.sh
#!/bin/bash n=1 while [ $n -le 5 ] do n=$(( $n + 1 )) user=user$n userdel -r $user echo "$user ...