一、题目

  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. layui开始时间小于结束时间

    直接上代码 // var endDate= laydate.render({ // elem: '#end_time',//选择器结束时间 // type: 'datetime', // min:&q ...

  2. iOS UITextView自适应高度UITextContainerView抖动问题

    在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scr ...

  3. Selenium | 简单使用

    需求分析: 登录百度首页,对百度首页进行截屏操作,保存文件 核心代码如下: //配置浏览器 System.setProperty("webdriver.chrome.driver" ...

  4. JDBC——入门知识【转】

      1. 什么是JDBC:Java数据库连接性(JavaDatabase Connectivity) API,允许用户从Java应用程序中访问任何表格化数据源. 2. JDBC除了提供到更宽范围的SQ ...

  5. 【ADO.NET】 使用通用数据库操作类Database (SQL Server)

    一.Web.config配置 <connectionStrings> <add name="constr_name" connectionString=" ...

  6. C#局部类型partial在定义实体类Model中的应用

    以前一直用继承类的方法,原来还可以这样 //例如:定义一个Person的实体类,用户ID(PersonId),姓名(Name),性别(Sex),年龄(Age),地址(Address),联系方式(Tel ...

  7. Handler引起的内存泄露

    一般我都写handler的时候是这样的:   public class MyActivity extends Activity{ private final Handler myHandler = n ...

  8. 在phpnow中配置phpunit

    前面都好了之后,在 D:\phpnow\php-5.2.14-Win32\PEAR 之外的地方执行 phpinfo 都会出现以下错误 Warning: require_once(File/Iterat ...

  9. SQL 导出csv

    https://jingyan.baidu.com/album/4b07be3c466b5d48b280f37f.html?picindex=9

  10. swift版本拼图游戏项目源码

    现学现做的第一个swift版本拼图游戏demo 常规模式,对换模式任你选择, 用到了花瓣的API,各种萌妹子~