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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  3. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. lettcode笔记--Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  6. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

  7. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. NGUI实现ScrollView功能

    NGUI,目前Unity3D Assert Store中最火的2D图形界面工具. 本文假设读者有Unity3D使用经验.有基本了解.NGUI3.6.0 1.新建Pannel(Scroll View), ...

  2. icmp_ping学习笔记

    1.用字符串指针做为发送缓冲区和接收缓冲区的指针: 2.icmp报文类型结构体可自行定义,也可用<netinet/ip_icmp.h>中定义好的strcut icmp结构体: 3.ip_h ...

  3. sql server 表中的编号自增

    http://jingyan.baidu.com/article/fec4bce244f902f2608d8b7a.html

  4. 自定义ANDROID中EDITTEXT中的HINT文本的大小

    EditText editText = (EditText) rootView.findViewById(R.id.et); // 新建一个可以添加属性的文本对象 SpannableString ss ...

  5. Android深度探索HAL与驱动开发 第三章 Git入门

    Git功能十分复杂,简单来说它使你的开发更为快捷和可控,尤其是在开源项目上展现的友好的交互和回馈. 熟悉一些git指令操作对开发者的帮助可以避免开发者受到一些外在因素打断开发进度,甚至延误项目的che ...

  6. linux之基础命令大全

    作为测试人员对linux系统命令必须非常熟悉,尤其对于高级测试工程师,从事性能方面测试,就更需要对linux命令了如指掌,这里只对部分常用命令做解释,想深入学习请关注后续文章 ctrl z  终止当前 ...

  7. ADB命令详解

    一.adb介绍 ADB的全称为Android Debug Bridge,字面意思就是安卓调试桥接,简单点说,它是Android系统提供的一套工具,通过它,我们可以在电脑上建立一个连接到手机的通道,然后 ...

  8. .split()函数使用方法

    split说明 split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list) split语法 str.split('type',num)[n] 整个语法的意思是:以 ...

  9. js控制 固定框架内图片 按比例显示 以及 占满框架 居中显示

    js控制 固定框架内图片 等比例显示 以及 占满框架 纵横居中显示 通过设置 js函数 fitDiv里面var fit的值就好 function fitDiv (obj) { var target_w ...

  10. C# 更新SQL Server数据库备注信息从另一数据库

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...