LeetCode 20 Valid Parentheses (括号匹配问题)
package leetcode_50; import java.util.Stack; /***
*
* @author pengfei_zheng
* 括号匹配问题
*/
public class Solution20 {
public static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
int len = s.length();
for(int i=0;i<len;i++){
char c = s.charAt(i);
if(c=='(' || c=='{' || c=='[') stack.push(c);
if(c==')'){
if(stack.isEmpty())
return false;
else if('('!=stack.pop())
return false;
}
if(c=='}'){
if(stack.isEmpty())
return false;
else if('{'!=stack.pop())
return false;
}
if(c==']'){
if(stack.isEmpty())
return false;
else if('['!=stack.pop())
return false;
}
}
if(!stack.isEmpty())
return false;
else
return true;
}
public static void main(String[]args){
String s="[[(])]";
System.out.println(isValid(s));
}
}
参考代码2:
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for (char c : s.toCharArray()) {
if (c == '(')
stack.push(')');
else if (c == '{')
stack.push('}');
else if (c == '[')
stack.push(']');
else if (stack.isEmpty() || stack.pop() != c)
return false;
}
return stack.isEmpty();
}
LeetCode 20 Valid Parentheses (括号匹配问题)的更多相关文章
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- 20. Valid Parentheses(括号匹配,用桟)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 20. Valid Parentheses - 括号匹配验证
Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- 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有效括号序列
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有效的括号
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 ...
随机推荐
- Linux入门基础教程之Linux系统简介
Linux的历史: Multics项目开发不顺利,贝尔实验室退出该项目后,开发了Unix,商业化后价格昂贵.Dennis Ritchie和Ken Thompson开发了Unix内核以及C语言.Andr ...
- 一款CSS3仿Google Play的垂直菜单
之前分享过一款非常酷的CSS3垂直下拉动画菜单,是多级菜单.今天我们来看一款也是用CSS3制作的垂直菜单,是仿Google Play的菜单,菜单项都带有可爱的小图标,可以先来看看效果图: 当然你可以在 ...
- 在Centos7下发布.NET CORE项目[转]
1.安装安装前准备开发环境 编译类库:yum -y install gcc make gcc-c++ openssl-devel 系统信息: CentOS Linux release 7.2.1511 ...
- c/c++ 代码中使用sse指令集加速
使用SSE指令,首先要了解这一类用于进行初始化加载数据以及将暂存器的数据保存到内存相关的指令, 我们知道,大多数SSE指令是使用的xmm0到xmm8的暂存器,那么使用之前,就需要将数据从内存加载到这些 ...
- position absolute定位之所属的containing box
http://www.w3.org/TR/CSS2/visudet.html#containing-block-details http://www.zhihu.com/question/199267 ...
- phonegap入门–2 Android phonegap工程建立
一.环境要求: 需要安装Android ADT 二.支持Android相关设备列表: a)Android 2.1 (Deprecated May 2013) b)Android 2.2 c)Andro ...
- beautifulsoup4 安装教程
下载beautifulsoup, 下载地址:https://www.crummy.com/software/BeautifulSoup/bs4/download/ 下载完成之后,解压到一个文件夹,用c ...
- CentOS下安装高版本GCC
CentOS下安装高版本GCC 微信分享: 有时编译需要用到4.8以上版本的GCC,由于CentOS源没有提供高版本的GCC安装包,这时就不能通过安装包安装.通常的解决方案就是通过编译安装高版本的 ...
- [Ubuntu] 关于使用 root 账号登录
(本文验证环境为 Ubuntu 14.04 和 Lubuntu 13.04) Ubuntu 维护者们认为实在没有必要使用 root 帐户,因为你想做的所有事情管理员都可以完成,管理员只需使用 sudo ...
- [Ubuntu] arp-scan - 扫描网络设备
使用arp-scan扫描所有网络设备信息. 1. 安装arp-scan ifantastic@ubuntu:~$ sudo apt-get install arp-scan 2. 扫描网络所有设备 i ...