一、题目

  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. 哈密顿图 BestCoder Round #53 (div.2) 1003 Rikka with Graph II

    题目传送门 题意:判断是否为哈密顿图 分析:首先一种情况是不合法的:也就是度数为1的点超过2个:合法的有:,那么从度数为1的点开始深搜,如果存在一种走法能够走完n个点那么存在哈密顿路 收获:学习资料 ...

  2. Python: How to iterate list in reverse order

    #1 for index, val in enumerate(reversed(list)): print len(list) - index - 1, val #2 def reverse_enum ...

  3. Android Studio编译开源项目(含NDK开发)常见报错

    1.未设置NDK的路径 Error:Execution failed for task ':library:ndkBuild'. > A problem occurred starting pr ...

  4. D. Tavas and Malekas DFS模拟 + kmp + hash || kmp + hash

    http://codeforces.com/contest/535/problem/D 如果真的要把m个串覆盖上一个串上面,是可以得,不会超时. 要注意到一点,全部覆盖后再判断时候合法,和边放边判断, ...

  5. 机器学习概念之特征处理(Feature processing)

    不多说,直接上干货! 肯定也有不少博友,跟我一样,刚开始接触的时候,会对这三个概念混淆. 以下是,特征处理.特征提取.特征转换和特征选择的区别! 特征处理主要包含三个方面:特征提取.特征转换和特征选择 ...

  6. 初识node,原理与浏览器何其相似

    话不多说,直接上图. 今日入手开始学习Nodejs,加油吧,小小前端的大V梦ヾ(◍°∇°◍)ノ゙

  7. PHP获取时间总结

    查询前一年时间戳 mktime(0,0,0,date('m'),date('d'),date('Y')-1); strtotime('-12 month'); 查询前6个月时间戳 mktime(0,0 ...

  8. Ubuntu系统下配置PHP支持SQLServer 2005

    最近在做一个项目,该项目的数据库是微软公司的的SQLserver ,数据库安装在另一台windows服务器上,而项目却部署在ubuntu server上.那么这样就会涉及到项目在linux上如何链接S ...

  9. ES5函数新增的方法(call、apply、bind)

    1.call()的使用<script type="text/javascript"> var obj1 = { name:'bob', fn:function(){ c ...

  10. LibreOJ #119. 最短路 (堆优化dijkstra)

    题目描述 给一个 n(1≤2500≤n) n(1 \leq 2500 \leq n)n(1≤2500≤n) 个点 m(1≤6200≤m) m(1 \leq 6200 \leq m)m(1≤6200≤m ...