一、题目

  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

  An input string is valid if:

  Open brackets must be closed by the same type of brackets.

  Open brackets must be closed in the correct order.

  Note that an empty string is also considered valid.

  Example 1:

  Input: "()"

  Output: true

  Example 2:

  Input: "()[]{}"

  Output: true

  Example 3:

  Input: "(]"

  Output: false

  Example 4:

  Input: "([)]"

  Output: false

  Example 5:

  Input: "{[]}"

  Output: true

  二、解题思路

  1、利用List集合实现一个栈;

  2、将字符串s转换成字符数组,并循环遍历;

  3、如果字符为:"{、(、["中的一个,则存入集合中;

  4、如果字符为:"}、)、]"中的一个,则取出集合中最后一个元素进行比较;

  5、如能匹配上,则删除集合中最后一个元素,否则返回false;

  6、最后判断集合大小是否为0,如是则返回true。

  三、代码实现

  public boolean isValid(String s) {

  if ("".equals(s)) {

  return true;

  } else {

  Map parentheseMap = new HashMap();

  parentheseMap.put(')', '(');

  parentheseMap.put(']', '[');

  parentheseMap.put('}', '{');

  char[] sArr = s.toCharArray();

  List stackList = new ArrayList();

  for (int i = 0; i < sArr.length; i++) {

  if (sArr[i] == '(' || sArr[i] == '[' || sArr[i] == '{') {

  stackList.add(sArr[i]);

  } else {无锡人流多少钱 http://wapyyk.39.net/wx/zonghe/fc96e.html

  if (stackList.size() == 0) {

  return false;

  } else {

  char temp = stackList.get(stackList.size() - 1);

  if (temp == parentheseMap.get(sArr[i])) {

  stackList.remove(stackList.size() - 1);

  } else {

  return false;

  }

  }

  return stackList.size() == 0 ? true : false;

  }

  }

由Java实现Valid Parentheses的更多相关文章

  1. LeetCode第[20]题(Java):Valid Parentheses

    题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...

  2. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  4. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  5. Longest Valid Parentheses leetcode java

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  6. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  7. 32. Longest Valid Parentheses (JAVA)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 32. Longest Valid Parentheses

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  9. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

随机推荐

  1. web前端图片预加载

    是什么? 浏览器会缓存静态资源(hmtl/css/img等).图片预加载就是让浏览器提前缓存图片,提升用户体验. 浏览器什么情况下会下载图片? 1,解析到html中img的src属性的时候 2,解析到 ...

  2. Centos 6.x 搭建 Zabbix Server

      zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让 ...

  3. 线段树(区间合并) HDOJ 3308 LCIS

    题目传送门 题意:线段树操作:1. 单点更新 2. 求区间的LCIS(longest consecutive increasing subsequence) 分析:注意是连续的子序列,就是简单的区间合 ...

  4. E. Anton and Tree 数组开大点

    http://codeforces.com/contest/734/problem/E 看了题解,缩点 + 树的直径. 然而一直wa14. 注意到, 缩点后重建图,在5的时候,5和6建了一条边,然后6 ...

  5. 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:解决

    严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain ...

  6. IOS 根据身份证号码获取 年龄 生日 性别

    /** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...

  7. Understanding Scroll Views 深入理解 scroll view 读书笔记

    Understanding Scroll Views 深入理解 scroll view  读书笔记   It may be hard to believe, but a UIScrollView is ...

  8. loadrunner:文本检查点web_reg_find和web_find两个函数的区别

    web_reg_find是先注册(register)后查找的:使用时将它放在请求语句的前面. 而web_find是查找前面的请求结果:使用时将它放在请求语句的后面. 另二者的参数也完成不一样的,web ...

  9. js实现ctrl+v粘贴并上传图片

    前端页面: <textarea class="scroll" id="text" placeholder="在此输入...">& ...

  10. centos7 取消Ctrl+Alt+Del重启功能

    转载:http://www.cnblogs.com/huangjc/p/4536620.html Linux默认允许任何人按下Ctrl+Alt+Del来重启系统.但是在生产环境中,应该停用按下Ctrl ...