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 ...
随机推荐
- <T extends Serializable>这是什么意思呢?看明白这个,你的问题就自然而然的明白了!
1.转自:https://blog.csdn.net/liwenqiang758/article/details/8131185 自己动手丰衣足食!!! 泛型是Java SE 1.5的新特性,泛型的本 ...
- Triangle 1.6 (A Two-Dimensional Quality Mesh Generator and Delaunay Triangulator)
Triangle 一个二维高质量网格(mesh)生成器和Delaunay三角化工具. PSLG(Planar Straight Line Graph)约束Delaunay三角网(CDT)与Delaun ...
- 爱,死亡和机器人 第十四集 齐马蓝 中文字幕(Python处理utf8文件获取想要的内容)
处理代码 file = "a.srt" fi = open(file, mode='r') a = fi.readline() i = 1 while len(str(a)) != ...
- 定时器篇---java.util.TimerTask和quartz
最近项目中出现了定时执行任务的东西,研究了一下,觉得挺不错的,以后还用得到,就总结了下. 这里只介绍两种java.util.Timer 和 quartz java.util.Timer java自带的 ...
- Ad_hoc_polymorphism 备份
https://en.wikipedia.org/wiki/Polymorphism_(computer_science) https://en.wikipedia.org/wiki/Ad_hoc_p ...
- RXSwift源码浅析(一)
简述 最近老大给了个新项目,我打算用Swift写.原来OC用的RAC,换到Swift自然框架也想试试新的,就用了RXSwift,对于这两个框架,我都是会用,但不解其中的原理,正好最近需求没下来,就研究 ...
- C#的split函数分割
C#的split函数分割 string str = textBox1.Text; string[] strlist = str.Split("\r\n".ToCharArray() ...
- jquery获取元素内容-text()和val()
不传参数的text()方法在获取文本内容时,会把子元素的文本也获取过来(会删除 HTML 标记),例子: <!doctype html> <html> <head> ...
- 取/etc/password文件最后一个单词的最后一个字符
三种方法都可以 [root@localhost ~]# sed -n "1,5 s#.*\(.\)#\1#p" /etc/passwd [root@localhost ~]# se ...
- Nginx全局变量
一.全局变量 $args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有参数,值为"?&qu ...