LeetCode -- Valid Parenthese
Question:
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.
Analysis:
问题描述:给出一个字符串,包含'(', ')', '{', '}', '[' and ']'确定它是否是有效地匹配括号。
思路:一看到括号匹配问题肯定想到用栈。遇到左括号就进栈;遇到右括号若栈顶元素与之匹配则POP出栈顶元素,若不匹配则返回false。
注意特殊情况:如"{}[]()", 或者“{[}]”, 或者“{{}}{{”等情况。
Answer:
public class Solution {
public boolean isValid(String s) {
char[] ch = s.toCharArray();
int n = ch.length;
if(ch.length == 0 || ch.length % 2 != 0)
return false;
if(ch[0] == '}' || ch[0] == ']' || ch[0] == ')')
return false;
Stack<Character> st = new Stack<Character>();
st.add(ch[0]);
int i = 1;
while(!st.isEmpty() && i < n) {
if(ch[i] == '{' || ch[i] == '[' || ch[i] == '(') {
st.add(ch[i]);
i++;
} else {
if(st.isEmpty())
return false;
char c = st.pop();
if(c == '{' && ch[i] != '}' || c == '[' && ch[i] != ']'
|| c == '(' && ch[i] != ')')
return false;
i++;
if(i < n && (ch[i] == '{' || ch[i] == '[' || ch[i] == '(')) {
st.add(ch[i]);
i++;
}
}
}
System.out.println(i +" " +st.size());
if(!st.isEmpty() || i < n - 1)
return false;
return true;
}
}
LeetCode -- Valid Parenthese的更多相关文章
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode——Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- C-net总结
SMB服务器信息块 DHCP动态主机配置协议 STMP简单邮件传输协议 POP(邮件协议) Gnutella 网络分析数据 nslookup DNS(域名系统) 请求注释(RFC)文件 ...
- Webpack4 学习笔记二 CSS模块转换
前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack 打包css模块 webpack是js模块打包器, 如果在入口文件引入css文件或其它的less.sass等文件,需要 ...
- Problem 1004-2017 ACM/ICPC Asia Regional Shenyang Online
题目来源:array array array Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Java分享笔记:使用entrySet方法获取Map集合中的元素
/*--------------------------------- 使用entrySet方法取出Map集合中的元素: ....该方法是将Map集合中key与value的关系存入到了Set集合中,这 ...
- cordova创建工程添加插件
创建工程 phonegap创建工程 代码 用以创建自己需要的 工程名 ; 报名 ;类名 ; 应用名 cordova create hello com.example.hello HelloWo ...
- centos6 下查看SELinux状态 关闭SELinux
转载自:https://blog.csdn.net/boomjane_testingblog/article/details/52859977 SELinux(Security-Enhanced Li ...
- 深入理解java虚拟机学习笔记(二)垃圾回收策略
上篇文章介绍了JVM内存模型的相关知识,其实还有些内容可以更深入的介绍下,比如运行时常量池的动态插入,直接内存等,后期抽空再完善下上篇博客,今天来介绍下JVM中的一些垃圾回收策略. 一. ...
- dts--tests(一)
cmdline.py """ DPDK Test suite. Test cmdline. """ import utils from te ...
- 【转载】最长回文字符串(manacher算法)
原文转载自:http://blog.csdn.net/lsjseu/article/details/9990539 偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid ...
- python中的集合内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- #集合性质:需要传入一个list,且不含重复的元素,无序 list_1=[1,2,1,4,5,8,3,4 ...