1、题目

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

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

2、思路

用stack来实现,左边的进栈,如果是右边的出栈,发现没有或者不匹配就返回false

3、代码实现

public class Solution {public boolean isValid(String s) {if (s == null || s.length() == 0) {return false;}char chars[] = s.toCharArray();Stack<Character> stack = new Stack<Character>();for (int i = 0; i < chars.length; ++i) {if (chars[i] == '(' || chars[i] == '{' || chars[i] == '[') {stack.add(chars[i]);} else {if (!stack.isEmpty()) {Character value = stack.pop();if (chars[i] == ')') {if (value != '(') return false;}if (chars[i] == '}') {if (value != '{') return false;}if (chars[i] == ']') {if (value != '[') return false;}} else {return false;}}}return stack.isEmpty();}
}

LeetCode之Valid Parentheses相关推荐

  1. LeetCode Longest Valid Parentheses

    原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...

  2. 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...

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

  3. Leetcode: Longest Valid Parentheses

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

  4. LeetCode: 20. Valid Parentheses

    0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...

  5. [LeetCode]--20. Valid Parentheses

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

  6. LeetCode 20 Valid Parentheses (C++)

    问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  7. [swift] LeetCode 20. Valid Parentheses

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

  8. [Leetcode] Longest Valid Parentheses

    找出字串裡最長的合法括號組 簡單說,一樣stack搜尋合法parenth,把不合法的 ( & ) index 紀錄下來,最後算index間的差值取最大就是最長的 public class So ...

  9. LeetCode 20. Valid Parentheses(c++)

    利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...

最新文章

  1. 重启服务才可连接BOOT服务器
  2. Python+Django+SAE系列教程14-----使表单更安全
  3. 【实用】用QuickViewer收集数据
  4. boost::statechart模块实现无效结果分配测试
  5. 通过java api操作hdfs(kerberos认证)
  6. 如何设计实现一个地址反解析服务?
  7. Kubernetes:实现应用不停机更新
  8. python基础小白题3
  9. php unicode 插入 mysql_关于MySQL的一些骚操作——提升正确性,抠点性能
  10. GBDT是如何成为推荐系统顶级工具人的?
  11. MSsql server里日期不能直接存入date字段,要转换一下
  12. android room 线程,Android协程——RoomCoroutines-Go语言中文社区
  13. 跟我学Android之二 第一个程序
  14. python打包和添加数据文件_使用pyinstaller 2.1将python打包并添加版本信息和图标
  15. Verilog Language--Modules:Hierachy--Module add
  16. 免费的两种https证书申请和安装
  17. matlab命令行窗口显示长度设置_设置命令行窗口输出显示格式 | MATLAB format| MathWork...
  18. java 包的位置_通过Java在jar文件所在的位置创建目录
  19. 2022-2028年中国工业控制阀行业市场行情动态及发展趋向分析报告
  20. 一文梳理类脑计算的前世今生 | 中科院自动化所研究员李国齐

热门文章

  1. 【全】.net core平台单元/集成测试结果、覆盖率、圈复杂度到可视化HTML报告之路...
  2. Hello Blazor:(11)全局截获事件执行
  3. 微软面向初学者的机器学习课程:3.1-构建使用ML模型的Web应用程序
  4. SQL Server CDC配合Kafka Connect监听数据变化
  5. asp.net core web api之异常
  6. 基于 abp vNext 和 .NET Core 开发博客项目
  7. 深入浅出 ASP.NET Core 与 Docker 入门课程说明
  8. 2019公众号总结之——Top100 技术文章汇总
  9. dotnet Blazor 用 C# 控制界面行为
  10. ASP.NET Core SignalR:集线器Hub