题目:
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]相关推荐

  1. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. 【java】浅谈注释
  2. 005_Redis的Hash数据类型
  3. access 提供程序无法确定object 值_Python | 加一行注释,让你的程序提速10+倍!numba库十分钟上手指南...
  4. Linux下创建动态库与使用
  5. 有了二叉查找树、平衡树为啥还需要红黑树?
  6. java8 block_java8的CompletableFuture使用实例
  7. c语言贪吃蛇游戏编程视频教程,C语言贪吃蛇游戏精典源码 - 视频教程 - VC中文网-VC-MFC编程论坛 - Powered by Discuz!...
  8. Python: sorted() 函数
  9. HDU3068 回文串 Manacher算法
  10. 在Scala IDEA for Eclipse或IDEA里程序编译实现与在Spark Shell下的对比(其实就是那么一回事)...
  11. python的画图工具有哪些_python实现画图工具
  12. Linux PHP开发环境:CentOS下配置LAMP开发环境
  13. 图像和音频格式解析一览
  14. iview在table中添加图片
  15. mysql的连接名和用户名_MySQL登陆认证用户名先后顺序
  16. 八、血条的制作和boss敌人的产生(雷霆战机)
  17. ARFoundation
  18. 水清冷冷:AutoCAD 2018安装图文教程方法(附工具),windows系统X64
  19. python:读取Excel文件
  20. 30万精英人才大迁徙:进军区块链,他们是赌徒还是信徒?

热门文章

  1. 特殊方法求1~n的和
  2. 指令和伪指令和宏指令区别
  3. Redis 基本数据类型 :String、Hash、List、Set、ZSet
  4. 手写实现Spring(IOC、DI),SpringMVC基础功能
  5. TCP的FIN_WAIT1状态理解|深入理解TCP
  6. C语言登顶!|2021年7月编程语言排行榜
  7. Java中已经存在了十几年的一个bug...
  8. OS--进程间通信详解(一)
  9. 应用深度学习使用 Tensorflow 对音频进行分类
  10. 全域调度:云边协同在视频场景下的探索实践