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 ...
随机推荐
- Cantor的数表
题目描述 如下数列,前5项分别是1/1,1/2,2/1,3/1,2/2…….输入n,输出第n项. 1/1 1/2 1/3 1/4 1/5 2/1 2/2 2/3 2/4 3 ...
- Swift—继承
一个类可以继承另一个类的方法,属性和其他特性.当一个类继承其他类时,继承类叫子类,被继承类叫超类(或父类).在Swift中,继承具有单继承的特点,每个子类只有一个直接父类,继承是区分类与其他类型的一个 ...
- oracle执行cmd的实现方法
网络上找到的在sqlplus中执行cmd的一些命令,主要有四种方法,这边都做了一下测试,这里做一下记录: 测试环境:window2003+Oracle 11.2.0.1.0 第一种方法: 最简单的执行 ...
- WebLogic 12c控制台上传获取webshell
WebLogic 默认端口7001,一般可以通过这样来访问 http://10.9.1.1:7001/console 控制台 通过弱口令weblogic/weblogic登录控制台,下一步获取如何上传 ...
- Codeforces Round #169 (Div. 2)
A. Lunch Rush 模拟. B. Little Girl and Game 因为可以打乱顺序,所以只关心每种数字打奇偶性. 若一开始就是回文,即奇数字母为0或1种,则先手获胜. 若奇数字母大于 ...
- [C++] Running time and Integer to String
std::string num2str(int64_t p_vint, int8_t p_radix) { char str[48] = { 0 }; int64_t temp = 0; int64_ ...
- ListView的item里面控件文本颜色修改
@SuppressLint("InflateParams") @Override public View getChildView(int groupPosition, int c ...
- 推荐一个Jenkins 插件 Build Failure Analyzer
Wiki https://wiki.jenkins-ci.org/display/JENKINS/Build+Failure+Analyzer 作用: 按照预定的build failure 去解析co ...
- MFC的几处坑
本人愚笨,一个滚动条控件还让我研究了几天,记录下成果. 1. GetScrollRange();获取滚动条的范围,个人觉得用意不大. GetScrollLimit();获取滚动按钮活动的范围 Scro ...
- 子句jion
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...