【LeetCode】20. Valid Parentheses 有效的括号
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
- 个人公众号:负雪明烛
- 本文关键词:有效,括号,括号匹配,栈,题解,leetcode, 力扣,Python, C++, Java
题目地址:https://leetcode.com/problems/valid-parentheses/#/description
题目描述
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.
题目大意
有三种括号组成的字符串,看最后的结果是否能组成正常的括号。
解题方法
Java解法
第一感觉就是栈,但是怎么用呢。这个方法就是如果左边的括号出现,那么把右边的括号放到栈里,这样,如果不是左边的括号出现时,弹出右边的括号,判断栈里边最后入的那个元素和目前的右边括号是否相同。如果不同就返回false。有种可能是入栈很多左括号,右括号个数小于左括号个数,所以最后也要判断一下栈是否为空,如果是空说明左右括号个数正好匹配。
public class Solution {
public boolean isValid(String s) {
if(s == null || (s.length() & 1) == 1){
return false;
}
Stack<Character> stack = new Stack<Character>();
for(char c : s.toCharArray()){
if(c == '('){
stack.push(')');
}else if(c == '['){
stack.push(']');
}else if(c == '{'){
stack.push('}');
}else if(stack.isEmpty() || stack.pop() != c){
return false;
}
}
return stack.isEmpty();
}
}
Python解法
二刷,Python,使用的也是栈。
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
for c in s:
if not stack:
stack.append(c)
else:
if c in ['(', '[', '{']:
stack.append(c)
else:
top = stack.pop()
if (top == '(' and c != ')') or \
(top == '[' and c != ']') or \
(top == '{' and c != '}'):
return False
return not stack
日期
2017 年 5 月 17 日
2018 年 11 月 24 日 —— 周六快乐
【LeetCode】20. Valid Parentheses 有效的括号的更多相关文章
- leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...
- [LeetCode]20. Valid Parentheses有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20 Valid Parentheses 有效的括号
描述: 给定一些列括号,判断其有效性,即左括号有对应的有括号,括号种类只为小,中,大括号. 解决: 用栈. bool isValid(string s) { stack<char> st; ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 20 Valid Parentheses 括号匹配
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- R shinydashboard——3.外观
目录 1.皮肤 2.注销面板 3.CSS 4. 标题延长 5.侧边栏宽度 6.图标 7.状态和颜色 1.皮肤 shinydashboard有很多颜色主题和外观的设置.默认为蓝色,可指定黑丝.紫色.绿色 ...
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
- 表格合并单元格【c#】
gridBranchInfo.DataSource = dtBranchViewList; gridBranchInfo.DataBind(); Random random = new Random( ...
- day 03Linux修改命令提示符
day 03Linux修改命令提示符 昨日回顾 1.选择客户机操作系统: Microsoft Windows # 一次只能安装一台电脑 Linux(推荐) VMware ESX # 服务器版本VNwa ...
- HTTP请求 Java API
1.导入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId>common ...
- Activity 详解
1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...
- 解决ViewPager与ScrollView 冲突
ViewPager来实现左右滑动切换tab,如果tab的某一项中嵌入了水平可滑动的View就会让你有些不爽,比如想滑动tab项中的可水平滑动的控件,却导致tab切换. 因为Android事件机制是从父 ...
- LinkBinTree
package ch11; import java.util.ArrayList; import java.util.List; import java.util.Stack; public clas ...
- Mybatis-运行原理
一.mybatis分层图 二.运行流程 根据全局配置文件创建sqlSessionFactory对象 根据全局配置文件的io流来构建SqlSessionFactoryBuilder对象: 解析(XmlC ...
- Mysql的索引调优详解:如何去创建索引以及避免索引失效
在正式介绍Mysql调优之前,先补充mysql的两种引擎 mysql逻辑分层 InnoDB:事务优先(适合高并发操作,行锁) MyISAM:性能优先(表锁) 查看使用的引擎: show variabl ...