蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目
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.
翻译
括号匹配
Hints
Related Topics: Stack, String
通过栈就可以简单实现了
代码
Java
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for(char c : s.toCharArray()){
if(c == '(') stack.push(')');
else if(c=='[') stack.push(']');
else if(c=='{') stack.push('}');
else if (stack.isEmpty()||stack.pop()!=c)
return false;
}
return stack.isEmpty();
}
}
Python
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for i in range(len(s)):
if s[i]=='(' or s[i]=='[' or s[i]=='{':
stack.append(s[i])
elif s[i]==')' or s[i]==']' or s[i]=='}':
if len(stack)==0: return False
tmp = stack.pop()
if (s[i]==')' and tmp!='(') or (s[i]==']' and tmp!='[') or (s[i]=='}' and tmp!='{'):
return False
if len(stack)!=0:
return False
return True
蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]的更多相关文章
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]
题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- JavaWeb基础—JSP
一.什么是JSP JSP 全称是 Java Server Pages,是一种开发动态web资源的技术 在原HTML上添加JAVA脚本(灵魂工程师,为页面添加灵魂),可以说 jsp = html + j ...
- MFC 消息映射、分派和传递
几个重要的结构体: struct AFX_MSGMAP { AFX_MSGMAP* pBaseMessageMap; AFX_MSGMAP_ENTRY* lpEntries; } struct AFX ...
- 12 stark组件之pop,按钮,url,页面
1.Window open() 方法 http://www.runoob.com/jsref/met-win-open.html 效果图 2.admin的pop添加按钮 3.stark之pop功能 ...
- Gitlab+Jenkins学习之路(七)之发布PHP项目
使用git+jenkins实现持续集成 Step1:构建一个自由风格的php-deploy Step2:Gernal配置,丢弃旧的构建,防止jenkins构建较多之后变臃肿 Step3:源码管理:这里 ...
- [HNOI2015]开店 树链剖分,主席树
[HNOI2015]开店 LG传送门 蒟蒻表示不会动态淀粉质. 先把点按年龄排序, 设\(dis[i]\)表示\(i\)到根的距离. 把我们要算的东西稍微变下形:\(ans\) \[ = \sum \ ...
- Security2:角色和权限
权限的授予分为三部分:权限(Permission),安全对象(Securable)和安全主体(Principal),这三个术语之间的关系是:Grant Permission on Securable ...
- 日常的例子说明 throttle 和 debounce 的区别
不小心接触到 throttle 和 debounce,按捺不住猎奇的心理,找这两个函数的资料. 然而百度到的各种对他们的理解,我去啊. 艰难地搞明白他们是干嘛的之后,忍不住举个例子说说自己的理解,希望 ...
- javascript中encodeURI和decodeURI方法使用介绍
encodeURI和decodeURI是成对来使用的,因为浏览器的地址栏有中文字符的话,可以会出现不可预期的错误, 所以可以encodeURI把非英文字符转化为英文编码,decodeURI可以用来把字 ...
- VRRP+tunnel+IP SLA+Track实现冗余切换
IP SLA(Internet Protocol Service-Level Agreement)互联网服务等级协议,本实验里通过发送测试报文,测试下一跳是否可达,结合Track实现冗余静态路由的切换 ...
- fiddler抓包工具教程
Fiddler是一个蛮好用的抓包工具,可以将网络传输发送与接受的数据包进行截获.重发.编辑.转存等操作.也可以用来检测网络安全.反正好处多多,举之不尽呀!当年学习的时候也蛮费劲,一些蛮实用隐藏的小功能 ...