Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目:
Given a string containing just the characters , determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
翻译:
给定一个字符串,只包含’(‘, ‘)’, ‘{‘, ‘}’, ‘[’ 和’]’这些字符,检查它是否是“有效”的。
括号必须以正确的顺序关闭,例如”()” 和”()[]{}”都是有效的,”(]” 和”([)]”是无效的。
分析:
本题考查的是栈结构,具有后进先出的特性。有效包含2个方面,第一个是如果是关闭的括号,前一位一定要刚好有一个开启的括号;第二个是最终结果,需要把所有开启的括号都抵消完。一个比较容易出错的地方是,遇到关闭括号时,要先判断栈是否已经空了。
Java版代码:
public class Solution {public boolean isValid(String s) {char[] charArr=s.toCharArray();List<Character> list=new ArrayList<>();for(Character c:charArr){if(c=='('||c=='{'||c=='['){list.add(c);}else{if(list.size()==0){return false;}char last=list.get(list.size()-1);if(c==')'&&last!='('){return false;}else if(c=='}'&&last!='{'){return false;}else if(c==']'&&last!='['){return false;}list.remove(list.size()-1);}}if(list.size()!=0){return false;}return true;}
}
Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]相关推荐
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Leet Code OJ 242. Valid Anagram [Difficulty: Easy]
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- Leet Code OJ 344. Reverse String [Difficulty: Easy]
题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...
- Leet Code OJ 28. Implement strStr() [Difficulty: Easy]
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- Leet Code OJ 1. Two Sum [Difficulty: Easy]
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- Leet Code OJ 223. Rectangle Area [Difficulty: Easy]
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- Leet Code OJ 189. Rotate Array [Difficulty: Easy]
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- Leet Code OJ 66. Plus One [Difficulty: Easy]
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
最新文章
- 【java】浅谈注释
- 005_Redis的Hash数据类型
- access 提供程序无法确定object 值_Python | 加一行注释,让你的程序提速10+倍!numba库十分钟上手指南...
- Linux下创建动态库与使用
- 有了二叉查找树、平衡树为啥还需要红黑树?
- java8 block_java8的CompletableFuture使用实例
- c语言贪吃蛇游戏编程视频教程,C语言贪吃蛇游戏精典源码 - 视频教程 - VC中文网-VC-MFC编程论坛 - Powered by Discuz!...
- Python: sorted() 函数
- HDU3068 回文串 Manacher算法
- 在Scala IDEA for Eclipse或IDEA里程序编译实现与在Spark Shell下的对比(其实就是那么一回事)...
- python的画图工具有哪些_python实现画图工具
- Linux PHP开发环境:CentOS下配置LAMP开发环境
- 图像和音频格式解析一览
- iview在table中添加图片
- mysql的连接名和用户名_MySQL登陆认证用户名先后顺序
- 八、血条的制作和boss敌人的产生(雷霆战机)
- ARFoundation
- 水清冷冷:AutoCAD 2018安装图文教程方法(附工具),windows系统X64
- python:读取Excel文件
- 30万精英人才大迁徙:进军区块链,他们是赌徒还是信徒?