leetcode week 10
来源:互联网 发布:淘宝店铺宣传团队抽成 编辑:程序博客网 时间:2024/06/12 18:30
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.
Subscribe to see which companies asked this question.
问题描述:数据结构课上也做过的经典括号匹配问题。一共3种括号。
解题思路:可以用stack来解决。依次读取输入的符号,若为左括号,则压入stack;若为右括号,则pop出stack中第一个元素,看看是否匹配。符号读取完stack还有元素或者读取途中stack已经为空都要返回false。
#include <stack>class Solution {public: bool isValid(string s) { stack<char> p; for (int i=0; i<s.length(); i++) { switch (s[i]) { case '(': case '{': case '[': p.push(s[i]); break; case ')': if (p.empty() || p.top()!='(') return false; else p.pop(); break; case '}': if (p.empty() || p.top()!='{') return false; else p.pop(); break; case ']': if (p.empty() || p.top()!='[') return false; else p.pop(); break; default: ; // pass } } return p.empty() ; }};
0 0
- leetcode week 10
- LeetCode week 10 : Edit Distance
- [week 10][Leetcode][Dynamic Programming] Climbing Stairs
- LeetCode Week 0
- LeetCode Week 1
- LeetCode Week 6: IPO
- LeetCode Week 2
- leetcode 300(week 7)
- LeetCode Week 3
- LeetCode Week 4
- leetcode 576(week 12)
- leetcode 8(week 13)
- leetcode 108(week 14)
- [Leetcode] Week One ------ Array
- week 10
- week 10
- Week 10
- LeetCode 题解(Week 10): 467. Unique Substrings in Wraparound String
- ijkplayer播放器的read_thread数据接受线程在处理音频的时候如果首个数据包没有发现audio stream 以后就不会有音频数据
- 该Android studio 默认jdk版本
- Gear 360新款发布!预购有礼哦
- django 自定义分页与bootstrap分页结合
- 模拟memcpy
- leetcode week 10
- Cocopods安装使用和错误
- 软件测试工程师笔试题及答案(二)
- hadoop webUI上不能查看log日志输出信息
- iOS 自动构建套件
- ruby on rails 中使用devise
- <框架篇(1)>Spring框架中IoC的解读
- 查询速度提升300倍?MySQL的这个小技巧一定要掌握!
- Hibernate的clear(),flush(),evict()方法详解