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 ...
随机推荐
- 深入理解JVM(四)JVM性能监控与故障处理工具
4.2 JVM命令行工具 1.jps(JVM Process Status Tool):JVM进程查看工具,命令行进入到jdk的bin目录下,使用命令:jps -l/-q/-v/-m 2.jstat( ...
- gitlab 10汉化
记得备份 先检查一下版本,好下载对应的汉化包 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION 1)然后下载10.0.x.diff 文件到服务 ...
- selenium中动作链的使用
一.问题 我们有时候在使用selenium的时候,会遇到悬停后点击元素的操作,因此需要一个动作链来完成这个功能. 二.解决 从selenium的包中导入actionchains函数,利用xpath找到 ...
- 75.iOS内存管理
堆区和栈区 1.栈区:由编译器自动分配释放,函数的参数值,局部变量等值 2.堆区:一般由开发人员分配释放,若不释放,则可能会引起内存泄漏 NSString *string = @"abcd& ...
- 小白的CTF学习之路2——二进制数据基础与运算(上)
今天的第二更,被我拖到了傍晚,嘿嘿,二进制这方面让本就数学不好的我很头疼,所以研究了一段时间 在学习之前我们先了解几个问题: 32位是几个字节? 01011100对于十进制是多少? 00001111向 ...
- 包含复杂函数的excel 并下载
POI 版本: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...
- 第一节 —— vue2.0 环境安装,工程化开发
vue的开发有两种,一种是直接的在script标签里引入vue.js文件即可,这样子引入的话个人感觉做小型的多页面会比较舒坦,一旦做大型一点的项目,还是离不开webpack. 所以另一种方法也就是基于 ...
- Pycharm的激活码,亲测可用(20181223)
K03CHKJCFT-eyJsaWNlbnNlSWQiOiJLMDNDSEtKQ0ZUIiwibGljZW5zZWVOYW1lIjoibnNzIDEwMDEiLCJhc3NpZ25lZU5hbWUiO ...
- Android-Java-抽象类
定义抽象类,就一定会定义抽象方法,抽象方法没有方法体{},就证明抽象方法 是不运行的,抽象方法 是给子类继承覆盖运行的, 子类继承->抽象类 就必须覆盖抽象方法,否则编译都失败: 水果案例: 定 ...
- 如何修改config?
这几天在做给WCF做加密传输,结果当然是实现了加密传输,同时也发现了一个问题,有没有大神来答疑解惑一下. 事情是这样的. 在客户端的配置中,需要加入一个behavior,在config文件中是这样的. ...