LeetCode之Valid Parentheses
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、思路
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相关推荐
- LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/ 题目: Given a string containing just ...
- 最长有效括号子串长度 c语言,LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)...
题目描述: Given a string containing just the characters'(' and')', find the length of the longest valid ...
- Leetcode: Longest Valid Parentheses
Question Given a string containing just the characters '(' and ')', find the length of the longest v ...
- LeetCode: 20. Valid Parentheses
0509第1题(虽然是08做的,但这会已经09了) 题目 Given a string containing just the characters '(', ')', '{', '}', '[' a ...
- [LeetCode]--20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (C++)
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [swift] LeetCode 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] Longest Valid Parentheses
找出字串裡最長的合法括號組 簡單說,一樣stack搜尋合法parenth,把不合法的 ( & ) index 紀錄下來,最後算index間的差值取最大就是最長的 public class So ...
- LeetCode 20. Valid Parentheses(c++)
利用栈的操作,遇到"(","[","{"即进栈,遇到")","]","}"判断是 ...
最新文章
- 重启服务才可连接BOOT服务器
- Python+Django+SAE系列教程14-----使表单更安全
- 【实用】用QuickViewer收集数据
- boost::statechart模块实现无效结果分配测试
- 通过java api操作hdfs(kerberos认证)
- 如何设计实现一个地址反解析服务?
- Kubernetes:实现应用不停机更新
- python基础小白题3
- php unicode 插入 mysql_关于MySQL的一些骚操作——提升正确性,抠点性能
- GBDT是如何成为推荐系统顶级工具人的?
- MSsql server里日期不能直接存入date字段,要转换一下
- android room 线程,Android协程——RoomCoroutines-Go语言中文社区
- 跟我学Android之二 第一个程序
- python打包和添加数据文件_使用pyinstaller 2.1将python打包并添加版本信息和图标
- Verilog Language--Modules:Hierachy--Module add
- 免费的两种https证书申请和安装
- matlab命令行窗口显示长度设置_设置命令行窗口输出显示格式 | MATLAB format| MathWork...
- java 包的位置_通过Java在jar文件所在的位置创建目录
- 2022-2028年中国工业控制阀行业市场行情动态及发展趋向分析报告
- 一文梳理类脑计算的前世今生 | 中科院自动化所研究员李国齐
热门文章
- 【全】.net core平台单元/集成测试结果、覆盖率、圈复杂度到可视化HTML报告之路...
- Hello Blazor:(11)全局截获事件执行
- 微软面向初学者的机器学习课程:3.1-构建使用ML模型的Web应用程序
- SQL Server CDC配合Kafka Connect监听数据变化
- asp.net core web api之异常
- 基于 abp vNext 和 .NET Core 开发博客项目
- 深入浅出 ASP.NET Core 与 Docker 入门课程说明
- 2019公众号总结之——Top100 技术文章汇总
- dotnet Blazor 用 C# 控制界面行为
- ASP.NET Core SignalR:集线器Hub