蜗牛慢慢爬 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 ...
随机推荐
- 苏州Uber优步司机奖励政策(4月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 正则表达式简介及在C++11中的简单使用
正则表达式(regular expression)是计算机科学中的一个概念,又称规则表达式,通常简写为regex.regexp.RE.regexps.regexes.regexen. 正则表达式是一种 ...
- 10 ORM 多表操作 查询
1.子查询:基于对象的跨表查询 def query(request): """ 跨表查询: 1.基于对象查询 2.基于双下划线查询 3.聚合.分组查询 4. F Q 查询 ...
- 将windows上.net core 发布的程序部署到linux(ubantu等)上
首先在linux安装相应的.net core 环境,根据官方的示例安装即可:参考地址:https://dotnet.microsoft.com/learn/dotnet/hello-world-tut ...
- H5拖拽 构造拖拽及缩放 pdf文件转换为html预览
前言: 协助项目需要实现一个签名的功能. 功能说明:1.有文本签名和头像签名.2.头像签名需要实现可拖拽功能.3.需要展示的是pdf的文件并需要获取签名位于pdf文件的相对位置. 功能一:实现拖拽 思 ...
- 不把DB放进容器的理由
原文地址:http://www.tuicool.com/articles/6VbqeqQ 原文为英文,以下是笔者的个人总结. 此处的DB包括但不限于Redis.ElasticSearch. 1.数据安 ...
- Scrapy中的POST请求发送和递归爬取
POST请求发送 重写爬虫应用文件中继承Spider类的 类的里面的start_requests(self)这个方法 def start_requests(self): #请求的url post_ur ...
- 14-Dockerfile常用指令
下面列出了 Dockerfile 中最常用的指令,完整列表和说明可参看官方文档.FROM 指定 base 镜像. MAINTAINER设置镜像的作者,可以是任意字符串. COPY将文件从 build ...
- [C++]模板类和模板函数
参考: C++ 中模板使用详解 C++模板详解 概念 为了避免因重载函数定义不全面而带来的调用错误,引入了模板机制 定义 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模 ...
- SSH免密登录(并且免yes交互)
问题描述:主机A使用ssh协议远程主机B,默认会开启口令认证,即输入主机B对应用户的登录密码,并且第一次登录时,主机A需验证是否接受来自主机B的公钥,输入"yes/no"完成交互. ...