[leetcode]20. Valid Parentheses有效括号序列
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
题意:
给定一个括号序列,判断其是否合法。
思路:
指针i来扫给定字符串
对于字符串的每个char,若是左括号,入栈
若栈不为空&&栈顶元素与对应位置的右括号匹配,出栈
代码:
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(c == '(' || c =='{' || c=='['){
stack.push(c);
}
else if( c == ')' && !stack.empty() && stack.peek() =='('){
stack.pop();
}
else if( c == '}' && !stack.empty() && stack.peek() =='{'){
stack.pop();
}
else if( c == ']' && !stack.empty() && stack.peek() =='['){
stack.pop();
}
else{
return false;
}
}
return stack.isEmpty();
}
}
followup: Valid Parentheses 简化版:只有()一种括号,且input string里有别的字母,加减号。看括号是否是闭合。
)()()() ----> true
(+1^$#)(#$) ----> true
)( ----->false
(()#%33 ----->false
代码:
public class valid_parenthese_modified {
public boolean isValid(String s) {
int count = 0;
for (char c : s.toCharArray()) {
if (c == '(')
count++;
else if (c == ')') {
if (count == 0) // notes for the if-judge here
return false;
count--;
}
}
return count == 0;
}
}
[leetcode]20. Valid Parentheses有效括号序列的更多相关文章
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
- [LeetCode] 20. Valid Parentheses ☆
转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...
随机推荐
- ES6 基本语法
ECMAScript 6 简介 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScrip ...
- <realsense D400>同步采集深度图和彩色图
利用深度相机采集深度图和彩色图会面临一个问题,如何实现同步采集数据? 以下是我搜集到的两点方法: 1)高翔博士提到他的orbslam2教程有这么一步工作,具体目录为 example/RGBD/. (等 ...
- 不常用的vi命令
vi u 撤回ctrl+r 撤回的撤回 全文替换%s/old/new/g 指定行区间替换12,15s/old/new/g c替换前确认12,15s/old/new/gc 用#代替分隔符,用户关键字有/ ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- 微信小程序如何设置服务器配置
最近微信小程序在it界火了起来,公司也要求我们开始接触微信小程序,废话不多说直接从配置微信小程序开始 1,首先,登录 https://mp.weixin.qq.com,(这里默认你已经获取到微信小程序 ...
- Power Designer 转C#实体类方法
1.打开Power Designer菜单 Tools,选择如图 2.弹出方框中选择PD安装目录下的如图地址 3.object language选择正确目录后,可选如图语言,如C#.再填写name ...
- H5的本地存储技术及其与Cookie的比较
第一部分: H5的本地存储技术 HTML5 提供了两种在客户端存储数据的新方法.先看下面的例子: 例1:var mySelection = {name:"car", amount: ...
- 格式化hdfs后,hadoop集群启动hdfs,namenode启动成功,datanode未启动
集群格式化hdfs后,在主节点运行启动hdfs后,发现namenode启动了,而datanode没有启动,在其他节点上jps后没有datanode进程!原因: 当我们使用hdfs namenode - ...
- Redis-Sentinel 数据源配置
1.redis配置文件 : redis.properties # Redis settings #sentinel_node_1 redis.sentinel1.host=192.168.0.1 re ...
- Tools:apache部署https服务
转自:https://www.cnblogs.com/ccccwork/p/6529367.html 1.要搭建https,必须要具备的东西 1.超文本传输协议httpd(apache)和ssl模块( ...