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.

解题思路:

栈使用的典型题目;

解题步骤:

1、新建一个栈;

2、遍历字符串:

  (1)符号的左半边,则入栈;

  (2)符号的右半边,则pop出栈顶元素,如果栈为空,则返回false;

     判断此右半边和栈顶元素是否匹配;匹配继续;不匹配则返回false;

3、循环结束后栈为空,则true,不为空则false;

 class Solution {
public:
bool isValid(string s) {
stack<char> st;
int len = s.length(); for(int i = ; i < len; ++i) {
if (s[i] == ')' || s[i] == ']' || s[i] == '}') {
if (st.empty())
return false;
char c = st.top();
if ((c == '(' && s[i] != ')') || \
(c == '[' && s[i] != ']') || \
(c == '{' && s[i] != '}'))
return false;
st.pop(); } else {
st.push(s[i]);
}
} return st.empty();
}
};

附录:

常见符号ASCII码

C++常见容器使用及公式

【Leetcode】【Easy】Valid Parentheses的更多相关文章

  1. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

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

  5. 【leetcode刷题笔记】Longest Valid Parentheses

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

  6. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  7. C# 写 LeetCode easy #20 Valid Parentheses

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

  8. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

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

  9. LeetCode 20. 有效的括号(Valid Parentheses)

    20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...

  10. leetcode第31题--Longest Valid Parentheses

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

随机推荐

  1. 【算法笔记】B1011 A+B 和 C

    1011 A+B 和 C (15 分) 给定区间 [−2​31​​,2​31​​] 内的 3 个整数 A.B 和 C,请判断 A+B 是否大于 C. 输入格式: 输入第 1 行给出正整数 T (≤10 ...

  2. Java正则表达式-捕获组

    private Set<String> getCodes(String s) { Set<String> resultSet = new HashSet<>(); ...

  3. 在Maven打包war的时候包含空目录/空文件夹

    Maven打包的时候貌似默认会忽略空的文件夹,如果需要包含他们,则需要添加如下的插件配置: <plugins> <plugin> <artifactId>maven ...

  4. Mac上Node环境配置

    公司配备Mac笔记本,以前没用过mac开发项目,一开始依然是从node官网下载安装包,后来领导说最好是用brew安装软件,这样比较方便,安装和卸载,只要在命令行输入相应的 install 和 unin ...

  5. Android耗时操作

    No subscribers registered for event class com.test.MessageEvent import de.greenrobot.event.EventBus; ...

  6. oracle sql命令

    set time on; 设置时间 alter table flashback_test enable row movement; 开启行移 select * from flashback_test ...

  7. shell 语法

    1). 条件表达式语法信息    [ 1 -eq 1 ] && echo 1        <-- 表示条件成功,执行相应操作    [ 1 -eq 1 ] || echo 1  ...

  8. opencv + ffmpeg

    opencv2.4.13 与 ffmepg 3.0 一起是可以安装成功的.注意编译ffmpeg时, ./configure   --enable-shared 否则会报错. 另外,把以上组合换成ope ...

  9. 【C#】隐式类型var

    在.NET 3.0后微软引入了隐式类型var,编译器可以自动判断变量的类型,通过var这个隐式类型,可以提高开发人员的开发效率,很多时候可以不考虑对象的类型,编译器会自动帮我们判断 使用隐式类型和使用 ...

  10. poj 1028 Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31088   Accepted: 13933 ...