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 ...
随机推荐
- Java多线程——Lock&Condition
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. package ...
- Mac下终端使用密钥登录服务器
可行方法: mac终端输入 ssh-keygen 因为mac系统是类unix系统,linux系统是unix系统演变来的,所以呢,相当于在一个linux系统登录另外一个linux系统, 基本命令还是一样 ...
- 在PC上运行安卓(Android)应用程序的几个方法
三种方法: 1.在PC安装一个安卓模拟器,在模拟器里面运行apk: 2.虚拟机安装 Android x86 然后在此系统里运行: 3.利用谷歌chrome浏览器运行(这是一个新颖.有前途.激动人心的方 ...
- 小程序笔记四:表单提交form
index.wxml代码 <form bindsubmit="formSubmit" bindreset="formReset"> <view ...
- PGsql 基本用户权限操作
Ⅰ. 安装与初始账户密码修改 1. 安装 sudo apt-get install postgresql-9.4 2. 管理员身份打开pg sudo -u postgres psql sudo -u ...
- highcharts系列之xAxis
xAxis定义的是x坐标轴的配置选项.默认情况下,x轴指的是水平轴,特殊指定的时候也可以作为垂直的轴使用,在多坐标系中,xAxis是有多个配置好了的轴object的数组. 下面来看一下,xAxis常用 ...
- Java如何取得当前程序部署的服务器的IP
1.问题:之前使用InetAddress.getLocalHost().getHostAddress()时,在开发机测试可以得到192.168.0.18这样的IP.但部署到linux服务器以后, 这个 ...
- Windbg在软件调试中的应用
Windbg在软件调试中的应用 Windbg是微软提供的一款免费的,专门针对Windows应用程序的调试工具.借助于Windbg, 我们常见的软件问题:软件异常,死锁,内存泄漏等,就可以进行高效的排查 ...
- 【转】Java代码操作Redis的sentinel和Redis的集群Cluster操作
总共四台机器,crxy99,crxy98分别是主节点和从节点. crxy97和crxy96是两个监控此主从架构的sentinel节点. 直接看代码: 1 import org.junit.Test ...
- matplotlib使用GridSpec调整子图位置大小 (非对称的子图)
用matplotlib.pyplot的subplots命令可以很方便的画对称的子图,但是如果要画非对称的子图(如下)就需要用GridSpec命令来控制子图的位置和大小: 而上图的结构可以用一下两种方式 ...