蜗牛慢慢爬 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 ...
随机推荐
- 20155307 2016-2017-2 《Java程序设计》第9周学习总结
20155307 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC(Java DataBase Connectivity)即java数据库连接,是一种 ...
- 3503: [Cqoi2014]和谐矩阵
3503: [Cqoi2014]和谐矩阵 链接 分析: 对于每个点,可以列出一个方程a[i][j]=a[i][j-1]^a[i][j+1]^a[i-1][j]^a[i+1][j],于是可以列出n*m个 ...
- 3495: PA2010 Riddle
3495: PA2010 Riddle 链接 分析: 每个点要么建首都,要么不建,并且一个点建了,会导致一些点不能建.所以可以考虑2-sat. 但是如果在每个郡里两两连边,边数是n^2的. 考虑用前缀 ...
- Gitlab+Jenkins学习之路(六)之Jenkins部署、升级和备份
一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...
- Object C学习笔记6-如何在Windows环境搭建Object C开发环境
1. 安装编译环境 Object C和其他很多语言一样,都需要有一个编译器.Object C 是在GCC下编译的.GCC(GNU Compiler Collection,GNU编译器集合),是一套由 ...
- 解决在控制层springmvc框架发出的400状态的错误
错误场景: 错误分析: 这也是我第一次遇到这个类型的异常,400响应状态代表:客户端发出的请求中携带的参数与服务器端接受的参数类型不匹配,进一步就是说我后台的实体类中数据类型为Date,而前台传递过来 ...
- Springboot日记——核心编码篇
背景吐槽:想要让自己进阶一下,一定要有个可以拿出来秀的东西,所以要尝试写一个属于自己的网站或者平台.因此,我大概的看了一下springboot+Mybatis-plus+... 框架介绍 通常 SSM ...
- 我在华为,软件测试人员在工作中如何运用Linux?
从事过软件测试的小伙们就会明白会使用Linux是多么重要的一件事,工作时需要用到,面试时会被问到,简历中需要写到.对于软件测试人员来说,不需要你多么熟练使用Linux所有命令,也不需要你对Linux系 ...
- 判断浏览器是chrome,Opera,Safari,Mac
function(){ return { isSafari: (navigator.userAgent.indexOf('Safari')>=0 || navigator.userAgent.i ...
- linux_connect_mysql
原文来自 https://www.cnblogs.com/lywy510/p/3615710.html #include <stdio.h> #include <stdlib.h&g ...