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. 20172306 2018-2019 《Java程序设计与数据结构》第一周学习总结

    20172306 2018-2019 <Java程序设计与数据结构(下)>第一周学习总结 教材学习内容总结 第一章 概述 (程序=数据结构+算法 软件=程序+软件工程) 1.1 软件质量 ...

  2. 阿里云ECS装LAMP环境

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

  3. magento中Model创建以及该Model对于数据库的增删改查

    本文是按照magento英文文档照做与翻译的. Model层的实现是mvc框架的一个巨大的部分.它代表了你的应用的数据,或者说大多数应用没有数据是无用的.Magento的Model扮演着一个重要的角色 ...

  4. downLoad

    String root= ServletActionContext.getServletContext().getRealPath(File.separator).replace("\\&q ...

  5. google protobuf VC下的使用笔记

    1 使用protobuf 2.x 下载地址(3.x 在c++11 vs2017下报错) 源码 https://github.com/google/protobuf 或者直接下载 二进制文件 2 如果下 ...

  6. python_day12_css

    目录: 简介 选择器(selecttor) 常用属性 页面布局 一.简介 1.CSS 定义 CSS是Cascading Style Sheets的简称,中文称为层叠样式表. CSS 规则由两个主要的部 ...

  7. RQNOJ 2 开心的金明

    一道基础的01背包,要是不明白可以自己搜一下背包九讲,自己刚开始数组开小了,题目看串了行,找了半天,小错还是要格外注意的. #include <iostream> #include < ...

  8. 20175316盛茂淞 2018-2019-2 《Java程序设计》第5周学习总结

    20175316盛茂淞 2018-2019-2 <Java程序设计>第5周学习总结 教材学习内容总结 第六章 接口与实现. 何谓接口 接口:书上没有明确地给出接口的定义,我理解的接口就是一 ...

  9. python模块:xml.dom.minidom

    """Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...

  10. wordpress添加文章固定字段

    让wordpress的文章数据表 增加一个字段,使其能在文章编辑页能编辑,并能通过rest api 获取出来. 例:给文章加一个缩略图字段 litpic 首先 通过mysql 给文章表 wp_post ...