import java.util.Stack;

/**
* Source : https://oj.leetcode.com/problems/valid-parentheses/
*
* Created by lverpeng on 2017/7/11.
*
* * 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.
*
*/
public class ValiadParentheses { /**
* 括弧匹配,使用栈进行判断,遇到左括号入栈,遇到右括号判断出栈,如果匹配则出栈,不匹配返回
* 最后判断是否匹配完成,并判断栈是否为空
*
* @param str
* @return
*/
public boolean valiad (String str) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '(' || c == '[' || c == '{') {
stack.push(c);
} else if (c == ')' || c == ']' || c == '}') {
char pop = stack.pop();
if (!((pop == '(' && c == ')') || (pop == '[' && c == ']') || (pop == '{' && c == '}'))) {
return false;
}
} else {
return false;
}
}
return stack.empty();
} public static void main(String[] args) {
ValiadParentheses valiadParentheses = new ValiadParentheses();
System.out.println("true-------" + valiadParentheses.valiad("{}[]()"));
System.out.println("true-------" + valiadParentheses.valiad("{[()]}[]()"));
System.out.println("true-------" + valiadParentheses.valiad("{}[({})]()"));
System.out.println("false-------" + valiadParentheses.valiad("{}[[()]"));
System.out.println("false-------" + valiadParentheses.valiad("{}[][(]"));
System.out.println("false-------" + valiadParentheses.valiad("{}2[][]()[]")); } }

leetcode — valid-parentheses的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

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

  3. [Leetcode] valid parentheses 有效括号对

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

  4. LeetCode——Valid Parentheses

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

  5. [LeetCode] Valid Parentheses

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

  6. leetcode—Valid Parentheses

    1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...

  7. Python3解leetcode Valid Parentheses

    问题描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  8. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  9. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

  10. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

随机推荐

  1. 微软URLRewriter.dll的url重写在目标框架.Net Framework2.0、4.0和应用程序池经典模式、集成模式下的配置

    大家参考几篇园子里面的这篇文章: 文章1:微软URLRewriter.dll的url重写的简单使用 (讲解了使用UrlReWriter.dll的下载.web.config如何在目标框架2.0应用程序池 ...

  2. arguments.callee的作用及替换方案

    arguments.callee的作用 arguments 的主要用途是保存函数参数, 但这个对象还有一个名叫 callee 的属性,返回正被执行的 Function 对象,也就是所指定的 Funct ...

  3. 阿里云ECS装LAMP环境

    学生计划9.9买个一个ECS,要做PHP开发,所以搭建一个Lamp的环境 1.使用镜像,附件 sh-1.5.5附于文后. 2.一键安装 2.1 输入命令:chmod –R 777 sh-1.5.5 c ...

  4. docker容器的使用

    Docker客户端 docker客户端非常简单,我们可以直接输入docker命令来查看到Docker客户端的所有命令选项. runoob@ docker 可以通过命令docker command -- ...

  5. django by example 第四章 dashboard处html无法渲染问题

    描述: 实现django by example 代码时,第四章 dashboard处html无法渲染问题. 此时报错,NoReverseMatch at /account/login/, Error ...

  6. merge and saveorupdate

    首先 saveOrUpdate返回void 也就是什么都不返回 而merge会返回一个对象 merge 在执行session.merge(a)代码后,a对象仍然不是持久化状态,a对象仍然不会被关联到S ...

  7. Chrome浏览器用AdBlockPlus拦截百度广告

    一:安装AdBlockPlus插件,这个貌似要FQ安装,不知道可不可以本地安装: 二:在右侧的扩展那里找到ABP扩展,然后设置-高级-我的过滤列表栏-开始创建我的过滤列表: 三:在列表栏里添加 bai ...

  8. 六.使用python操作mysql数据库

    数据库的安装和连接 pymysql的安装              pip install PyMySQL python连接数据库 import pymysql db = pymysql.connec ...

  9. Dynamic Programming | Set 1 (Overlapping Subproblems Property)

    动态规划是这样一种算法范式:将复杂问题划分为子问题来求解,并且将子问题的结果保存下来以避免重复计算.如果一个问题拥有以下两种性质,则建议使用动态规划来求解. 1 重叠子问题(Overlapping S ...

  10. MySQL基础--字符函数

    1.UPPER和UCASE返回字符串str,根据当前字符集映射(缺省是ISO-8859-1 Latin1)把所有的字符改变成大写.该函数对多字节是可靠的. 2.LOWER和LCASE返回字符串str, ...