【LeetCode算法-20】Valid Parentheses
LeetCode第20题
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
思路:
本来我的想法是不管(),[],{},都是在一起的,我一对一对的删掉,最后删空了,就符合要求
代码
class Solution {
public boolean isValid(String s) {
if(s.length()>Integer.MAX_VALUE){
return true;
}
for(int i = 0;i<3;i++){
for(int j = 0;j<s.length();j++){
s = s.replace("()","");
System.out.println(s);
s = s.replace("[]","");
System.out.println(s);
s = s.replace("{}","");
System.out.println(s);
}
}
if("".equals(s)){
return true;
}else{
return false;
}
}
}
结果
搞这么多符号,这不故意整我吗
百度了下,都说用栈,代码如下
class Solution {
public boolean isValid(String s) {
Stack<String> stack = new Stack<String>();
for (int i = 0; i < s.length(); i++) {
char candidate = s.charAt(i);
if (candidate == '{' || candidate == '[' || candidate == '(') {
stack.push(candidate + "");
} else {
if (stack.isEmpty()) {
return false;
}
if ((candidate == '}' && stack.peek().equals("{")) ||
(candidate == ']' && stack.peek().equals("[")) ||
(candidate == ')' && stack.peek().equals("("))) {
stack.pop();
} else {
return false;
}
}
}
if (stack.isEmpty()) {
return true;
} else {
return false;
}
}
}
就是利用后进先出的原理,其实跟我的思路差不多,但是性能要好很多,哈哈
【LeetCode算法-20】Valid Parentheses的更多相关文章
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【算法】LeetCode算法题-Valid Parentheses
这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
- 【LeetCode】20. Valid Parentheses 有效的括号
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- LeetCode算法01 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【LeetCode】20. Valid Parentheses
题目:
随机推荐
- Google开发者大会:你不得不知的Tensorflow小技巧
Google开发者大会:你不得不知的Tensorflow小技巧 同步滚动:开 Google Development Days China 2018近日在中国召开了.非常遗憾,小编因为不可抗性因素滞 ...
- 改造 Android 官方架构组件 ViewModel
前言 Android 官方架构组件在今年 5 月份 Google I/O 大会上被公布, 直到 11 月份一直都是测试版, 由于工作比较繁忙, 期间我只是看过类似的文章, 但没有在实际项目中使用过, ...
- Confluence 6 配置手动备份
如果你希望关闭自动备份,你可以选择手动导出保存站点.请参考 Manually Backing Up the Site 页面中的内容获得更多的信息. 这些文件没有自动备份在同样的路径中,这些文件存储在 ...
- python之dict与set实现原理之hash算法
理解不透彻,下回分解 http://www.cnblogs.com/pengsixiong/p/5326893.html https://blog.csdn.net/zhao_crystal/arti ...
- hdu2460 e-DCC染色缩点+暴力LCA
/* 给定一个无向图,往里面加边,问加第i条边时图中的桥数 首先肯定要求初始状态下的桥,染色缩点 每次给定的边为(u,v), 那么u->lca(u,v)->v路上的所有边都不再是桥 求LC ...
- jQuery之导航菜单(点击该父节点时子节点显示,同时子节点的同级隐藏,但是同级的父节点始终显示)
注:对于同一个对象不超过3个操作的,可直接写成一行,超 过3个操作的建议每行写一个操作.这样可读性较强,可提高代码的可读性和可维护性 核心代码: $(".has_children" ...
- .gz解压
1.今天很神奇我遇到这样的压缩包,啧啧啧,好少见的,记录下 gzip -d http_log.gz 这是讲http_log文件解压到当前的路径下
- IDM的Google商店插件
官方扩展链接:https://chrome.google.com/webstore/detail/idm-integration-module/ngpampappnmepgilojfohadhhmbh ...
- python 垃圾回收
# 垃圾回收 # 小整数对象池 # a = 100# python对小整数的定义是[-5,257],这些证书对象是提前创建好的,不会被垃圾回收,再一个python的程序中,所有位于这个范围内的正式使用 ...
- PV-UV-QPS
QPS:每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力.QPS = req/sec = 请求数/秒QPS统计方式 [一般使用 http_load 进行统计]QP ...