[LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
来源:互联网 发布:mac的桌面壁纸 编辑:程序博客网 时间:2024/05/29 10:54
题目链接: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.
(1)思路:这和数据结构课本上Stack的括号匹配例题类似,只不过括号的类型有三种。遇到左括号直接入栈,遇到右括号进行匹配,匹配成功的左括号弹出栈,匹配不成功或最后栈不为空返回 false。
(2)代码:
class Solution {public: bool isValid(string s) { char* stackBot = (char*) malloc(s.length()); int i; int stacktop = -1; for (i = 0; i < s.length(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') stackBot[++stacktop] = s[i]; else if ((s[i] == ')' && stackBot[stacktop] == '(') || (s[i] == ']' && stackBot[stacktop] == '[') || (s[i] == '}' && stackBot[stacktop] == '{')) --stacktop; else { free(stackBot); return 0; } } free(stackBot); return stacktop == -1; }};
(3)提交结果:
阅读全文
0 0
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- [LeetCode-Algorithms-65] "Valid Number" (2017.11.1-WEEK9)
- LeetCode Algorithms 32. Longest Valid Parentheses
- LeetCode Algorithms 32. Longest Valid Parentheses 题解
- LeetCode 20: Valid Parentheses
- LeetCode(20) Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode【20】:Valid Parentheses
- leetcode 20-Valid Parentheses
- [leetcode 20] Valid Parentheses
- [Leetcode] 20 - Valid Parentheses
- LeetCode | #20 Valid Parentheses
- leetcode.20-----------Valid Parentheses
- [leetcode] 20 Valid Parentheses
- leetcode-20 Valid Parentheses
- leetCode #20 Valid Parentheses
- leetcode 20 Valid Parentheses
- Leetcode 20 Valid Parentheses
- PAT (Basic Level) Practise (中文)1013. 数素数 (20)
- k-近邻算法(一)
- java日常学习:构造函数练习
- 更新不要停 11.1号苹果带来iOS 11.1正式版
- 关于MPI补充之通信篇
- [LeetCode-Algorithms-20] "Valid Parentheses" (2017.11.1-WEEK9)
- Scrollview嵌套listVIew冲突问题的解决
- 005_LeetCode_5 Longest Palindromic Substring 题解
- day08-WorkAndStudy
- Python之缩进错误
- Java消息中间件学习笔记七 -- Spring中使用JMS
- cglib动态代理[基于类操作的动态代理实现]
- 数据结构-C语言 栈的简单应用——括号匹配
- 简图记录-linux内核同步基础