蜗牛慢慢爬 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 ...
随机推荐
- 2017-2018-1 20155234 实验三 实时系统及mypwd实现
2017-2018-1 20155234实验三实时系统及mypwd实现 实验三-并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号 ...
- 【ruby题目】以|为分割点,将arr转换为二维数组
#以|为分割点,将arr转换为二维数组 arr = ['] tmp = [] tmp2 = [] for x in arr tmp << x if x != '|' tmp2.push A ...
- JavaScript 的HTML转义方法 html_encode 和 html_decode
此方法用来将用户输入内容中的尖括号.引号等进行转义
- 利用Smith圆图设计匹配网络
要实现射频最大的功率传输,必须使负载阻抗与源阻抗相匹配(即信号源阻抗与负载阻抗共轭相等),实现匹配的通常做法是在源和负载之间插入一个匹配网络,该网络不仅仅为减少功率损耗而设计,还可减少噪声干扰.提高功 ...
- Python学习之路:NumPy初识
import numpy as np; //一维NumPy数组 myArray = np.array([1,2,3,4]); print(myArray); [1 2 3 4] //打印一维数组的形状 ...
- aws存储桶s3使用
关于aws s3的使用说明: aws官方文档地址:https://docs.aws.amazon.com/s3/index.html#lang/zh_cn 创建s3与基础使用: 1.登陆aws控制台- ...
- 阿里云centos7.4安装并部署svn1.10.0版本(配置多仓库,加入开机自启动)
如何安装最新版本 1.10.0: 如果已安装旧版本,先卸载 yum remove subversion* 查看当前可安装的版本 yum list | grep subversion 可以去官网下载安装 ...
- Tetris(俄罗斯方块)
一天有个小朋友问我OpenGL俄罗斯方块怎么写. 俄罗斯方块分成两部分游戏逻辑和画面渲染. 1. 游戏逻辑 一个简单的俄罗斯方块的逻辑部分需要考虑的情况如下: 1. 方块的表示(坐标, 旋转, 上下左 ...
- linux 开机报错,error grub_efi_find_mmap_size not find
开机报错,差点以为要重装系统了 搜到了官方的重建引导的教程 修复了错误 https://wiki.manjaro.org/index.php/Restore_the_GRUB_Bootloader#F ...
- 算法工程师进化-SQL
1 引言 SQL操作往往是程序员必备的技能,对于算法工程师而言,熟练掌握SQL操作则更为重要.本文以<SQL语句执行顺序>作为学习资料,总结SQL的理论部分. 2 SQL查询语句的执行顺序 ...