Problem: 括号匹配问题。
  使用栈,先进后出!
 
参考代码1:
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 (括号匹配问题)的更多相关文章

  1. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  2. 20. Valid Parentheses(括号匹配,用桟)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. 20. Valid Parentheses - 括号匹配验证

    Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. [LeetCode]20. Valid Parentheses有效的括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

随机推荐

  1. sqlserver修改主机名

    sqlserver迁移后,主机和原机器不符,将系统修改主机名后,数据库代理服务.邮件服务无法启动 执行下面语句,检查sqlserver中windows主机名 -- 检查SQL Server中的&quo ...

  2. 简单理解IoC与DI

    为了理解Spring的IoC与DI从网上查了很多资料,作为初学者,下面的描述应该是最详细,最易理解的方式了. 首先想说说IoC(Inversion of Control,控制倒转).这是spring的 ...

  3. vue-cli打包之后的项目在nginx的部署

    vue-cli执行 npm run build 进行打包,生成dist文件夹,把该文件夹下的文件直接复制到nginx服务器目录下,就可打开项目,但是只有首页是可以看到的,再刷新一下就404了,原因是v ...

  4. redis 的 HyperLogLog

    Redis 在 2.8.9 版本添加了 HyperLogLog 结构. Redis HyperLogLog 是用来做基数统计的算法 HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大 ...

  5. zabbix-agents客户端安装

    Windows下zabbix客户端的安装 记得关windows上的防火墙 1.首先需要下载zabbix_agents.rar文件 点击这里进行下载 当然也可以在www.zabbix.com进行下载 2 ...

  6. 如何重新排列数组使得数组左边为奇数,右边为偶数,并使得空间复杂度为O(1),时间复杂度为O(n)

    思路分析: 类似快速排序的处理.可以用两个指针分别指向数组的头和尾,头指针正向遍历数组,找到第一个偶数,尾指针逆向遍历数组,找到第一个奇数,使用引用参数传值交换两个指针指向的数字,然后两指针沿着相应的 ...

  7. ios开发之--两次模态弹出后,怎么返回最上层的页面

    解决方法如下: self.presentingViewController.view.alpha = ;[self.presentingViewController.presentingViewCon ...

  8. vux (scroller)上拉刷新、下拉加载更多

    1)比较关键的地方是要在 scroller 组件上里加一个 ref 属性 <scroller :lockX=true height="-170" :pulldown-conf ...

  9. Yarn执行流程

    在Yarn中,JobTracker被分为两部分:ResourceManager(RM)和ApplicationMaster(AM). MRv1主要由三部分组成:编程模型(API).数据处理引擎(Map ...

  10. 运行RF测试案例,显示unable to open socket to "localhost:56505" error: [Errno 10061] 错误,且关闭RF卡死的解决办法

    问题描述: 执行WEB ui测试案例后,执行请他的测试案例显示unable to open socket to "localhost:56505" error: [Errno 10 ...