LeetCode——Valid Parentheses
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.
原题链接:https://oj.leetcode.com/problems/valid-parentheses/
题目:给定一个仅包括'('
, ')'
, '{'
, '}'
, '['
和 ']' 的字符串,检測输入的串是否合法。括号是否配对。
思路:使用一个栈。遇到左括号则压入。遇到右括号则与左括号检測是否匹配,不匹配即false,弹出顶元素,循环。
public boolean isValid(String s){
int len = s.length();
if(len <= 1)
return false;
if(s.charAt(0) == ')' || s.charAt(0)==']' || s.charAt(0)=='}')
return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<len;i++){
if(s.charAt(i) == '(' || s.charAt(i)=='[' || s.charAt(i)=='{')
stack.push(s.charAt(i));
else{
if(stack.size() == 0)
return false;
if(s.charAt(i) == ')')
if(stack.peek() != '(')
return false;
if(s.charAt(i) == ']')
if(stack.peek() != '[')
return false;
if(s.charAt(i) == '}')
if(stack.peek() != '{')
return false;
stack.pop();
}
}
return stack.size() == 0;
}
LeetCode——Valid Parentheses的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- Python3解leetcode Valid Parentheses
问题描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- leetcode Valid Parentheses python
# 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
随机推荐
- [luogu P5349] 幂 解题报告 (分治FFT)
interlinkage: https://www.luogu.org/problemnew/show/P5349 description: solution: 设$g(x)=\sum_{n=0}^{ ...
- Oracle-基本SQL语句
--添加一个表 create table TestUser ( id int primary key , name varchar(20) , address varchar(20) ) /* *设置 ...
- Spark Streaming 整合 Kafka
一:通过设置检查点,实现单词计数的累加功能 object StatefulKafkaWCnt { /** * 第一个参数:聚合的key,就是单词 * 第二个参数:当前批次产生批次该单词在每一个分区出现 ...
- (转)ORA-01502
问题:ora-01502 索引或这类索引的分区处于不可用状态 引发:移动数据表分区,导致索引失效 解决:重建失效索引 1. select index_name ,status from user_i ...
- vs2012编译boost_1_54_0
在原文上进行了修改,我的环境是VS2012 ,在编译 注意事项:Boost 请慎用!微软太坑爹...且直接使用GitHub上的exe文件也可以,特定版本的只能自己编译了....汗!!! 原文地址:ht ...
- 实验6 Bezier曲线生成
1.实验目的: 了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法. 2.实验内容: (1) 结合示范代码了解曲线生成原理与算法实现,尤其是Bezier ...
- Python Tutorial笔记
Python Tutorial笔记 Python入门指南 中文版及官方英文链接: Python入门指南 (3.5.2) http://www.pythondoc.com/pythontutorial3 ...
- getopt函数
getopt -- 解析命令的可选项 [说明]getopt只是一个简单的解析命令可选项的函数,只能进行简单的格式命令解析,格式如下: 1.形如:cmd [-a][-b] //对短选项的解析: ...
- 优动漫PAINT(clip studio paint)怎么画一幅水墨竹子图
今天小编分享使用优动漫PAINT绘制一个水墨竹子教程,绘画的过程中我只用到了两个笔刷,即钢笔模式下的“美术字”和“效果线专用”,并且全程鼠标绘制哦,所以生疏的笔触效果大家见谅,没有数位板的小伙伴不妨试 ...
- Linux date命令的用法(转)
1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与时间. 3.命令参数 -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: -s<字符串& ...