leetcode — valid-parentheses
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的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- LeetCode——Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode—Valid Parentheses
1.问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if t ...
- Python3解leetcode Valid Parentheses
问题描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- leetcode Valid Parentheses python
# 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- Valid Parentheses [LeetCode 20]
1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
随机推荐
- 一不注意,在Unity3D中DllImport 引起的Bug.
单要说这个Bug是很简单,但是得从头说起. 一些大型的网络游戏,或者加载比较多的一些场景时,如果要等待所有模型,贴图等各种资源文件加载完毕才能执行游戏,对用户将会是一个很头大的事情.所以就需要用到动态 ...
- HTTP协议简单认识
一.HTTP协议简介 HTTP超文本传输协议是一种用于分布式,协作式和超媒体信息系统的应用层协议 二.HTTP协议概述 HTTP是一个客户端和服务端请求和响应的标准 三.HTTP协议工作步骤 1.客户 ...
- linux关机、重启命令
1.shutdown -h 10 //计算机将在10分钟后关机,且会显示在登录用户的当前屏幕中 2.shutdown -h now //立即关机 3.shutdown -h 20:25 //系统会在2 ...
- s4 docker 网络2进阶
多容器复杂应用的部署 基于 flask容器,链接外部另一个redis 容器 docker pull redis sudo docker run -d --name redis redis # redi ...
- 微信js sdk的使用初步理解
第一步引入js文件 在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.2.0.js 备注:支持使用 AMD/C ...
- Maths | 离散K-L变换/ 主成分分析法
目录 1. 概述 2. K-L变换方法和原理推导 2.1. 向量分解 2.2. 向量估计及其误差 2.3. 寻找最小误差对应的正交向量系 3. K-L变换高效率的本质 4. PCA在编.解码应用上的进 ...
- 03-jQuery动画效果
Query提供的一组网页中常见的动画效果,这些动画是标准的.有规律的效果:同时还提供给我们了自定义动画的功能. 显示动画 方式一: $("div").show(); 解释:无参数, ...
- 有关PHPhash算法
https://www.cnblogs.com/shangzekai/p/4444169.html
- mysql最基础命令
1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:2.创建一个数据库MYSQLDATAmysql> Create DATABASE MY ...
- 笔记:BroadcastReceiver的运行过程
广播概述 广播用来在组件之间传递消息,可以是同进程或跨进程. 广播机制是基于发布订阅的事件驱动模型,使用上比Binder通信(跨进程接口回调)更低耦合.简单. ActivityManagerServi ...