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 ...
随机推荐
- Android之ListView&Json加载网络数据
使用到的主要内容: 1.Json 解析网络数据 2.异步任务加载图片和数据 3.ListView 的内存空间优化(ConvertView)和运行时间优化(ViewHolder) 4.ListView ...
- MFC中换行实现
在mfc中编辑框允许输入多行时,换行符被表示为<归位><换行>即"\r\n",用ascii码表示为13 10 如果为编辑框中想要输入换行,就请将编辑框的属性 ...
- ORACLE执行详解
本文源自TTT BLOG,原文地址:http://blog.chinaunix.net/u3/107265/showart_2192657.html 简介: 本文全面详细介绍oracle执行计 ...
- JDBC查询指定条件的数据
使用select语句的条件查询,需要用到where子句. package qddx.JDBC; import java.sql.*; public class QueryById { public b ...
- 使用xib文件创建集合类单元格
UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的各 ...
- Your stream was neither an OLE2 stream, nor an OOXML stream.问题的解决
先说说问题的来源 ,使用NPOI读取Except,先通过流来读取,如果符合要求,就将流保存为文件. 众所周知,流只能读一次,所以在流读取之前需要将流拷贝一份,保存文件的时候使用. protected ...
- PE文件格式 持续更新ing
PE文件就是exe文件和dll文件,前者是可执行文件,后者是动态连接库文件.两者的区别仅仅是字面上的,唯一的区别就是内部的一个字段标识这个文件是exe文件还是dll文件. 对于PE文件格式,举一个例子 ...
- js问题
1.原型链问题 1.js中万物皆对象,但对象也分为普通对象和函数对象,Object,Function都是js自带的函数对象,凡是通过 new Function() 创建的对象都是函数对象,其他的都是普 ...
- 使用vue给导航栏添加链接
如下面的导航栏,使用vue技术给该导航栏增加链接: js代码为: navigation:function(){ new Vue({ el: '#navUl', data: { menuData:{ ' ...
- Reed-Solomon码,QR
原文: Reed–Solomon codes for coders参考: AN2407.pdfWIKI: 里德-所罗门码实现:Pypi ReedSolo #译注:最近看到了RS码,发现还挺有意思的,找 ...