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的更多相关文章

  1. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  2. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  3. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  4. [LeetCode] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  6. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  7. [LeetCode] Valid Parentheses 验证括号

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

  8. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. LeetCode——Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

随机推荐

  1. EBS补丁时遇到的问题

    今天在给R12.1.3打17525552的时候,出现了这样的一个错误 ATTENTION: All workers either have failed or are waiting: FAILED: ...

  2. 【期望dp 质因数分解】cf1139D. Steps to One

    有一种组合方向的考虑有没有dalao肯高抬啊? 题目大意 有一个初始为空的数组$a$,按照以下的流程进行操作: 在$1\cdots m$中等概率选出一个数$x$并添加到$a$的末尾 如果$a$中所有元 ...

  3. C#基础学习笔记(个人整理)

    学习笔记 第一章:c#基础 一.程序设计语言的发展及历史 1.程序设计语言? 通俗也叫编程语言,实现人与机器交互的工具 2.历史 1)机器语言 : 0,1 2)汇编语言 : 包含一些机器语言,同时增加 ...

  4. MySQL(mariadb)主从复制模式与复制过滤

    在前一篇文章<mysql多实例与复制应用>中只对mysql的复制做了简单的介绍,本篇内容专门介绍一下mysql的复制. MySQL复制 mysql复制是指将主数据库的DDL和DML操作通过 ...

  5. php图片压缩-高清晰度

    php高清晰度无损压缩 经常会用到把上传的大图片压缩,特别是体积,在微信等APP应用上,也默认都是有压缩的,那么,怎么样对图片大幅度压缩却仍能保持较高的清晰度呢? 压缩通常是有按比例缩放,和指定宽度压 ...

  6. 提高mysql性能(like搜索替换 )

    一 .mysql用find_in_set代替like搜索提高性能 SELECT * from mobantestinfo1 where find_in_set('33',info2); 二 .使用内部 ...

  7. 安装python虚拟运行环境,linux下轻松切换python2和python3

    一.查询系统采用的python版本 $ python --version Python 3.7.3 系统采用的python版本为3.7.3 以下查询py3和py2的目录: $ which python ...

  8. 笔记-git-基础使用

    笔记-git-基础使用 1.      git相关概念 工作区(Working Directory): 就是在电脑里能看到的目录,init后的当前目录就是一个工作区: 版本库(Repository): ...

  9. chrome 切换到其他网络后重新加载网

    chrome 突然提示 “您与 www.google.com 之间的安全连接目前正受到干扰.  请等待几分钟后再尝试重新加载网页,或在切换到其他网络后重新加载网” 查看证书发现 已经过期 解决:同步下 ...

  10. 网络策略中使用的 VLAN 属性

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 Win ...