LeetCode20:validParentheses
validParentheses
题目描述
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

思路
- 使用栈方法,遍历字符串
- 如果当前字符为左边括号时,直接将其压入栈
- 如果遇到右半边括号时,分类讨论:
- 1.如果栈不为空,验证是否对应左边的括号,取出栈顶元素,继续循环、
- 若这时栈为空,则直接返回false
- 若不为对应的左边括号,返回false
实现思路
1.使用栈方法(使用数组的push()和pop()来模拟)
代码
var isValid = function(s) {
let valid = true
const stack = []
const mapper = {
'{': '}',
'(': ')',
'[': ']'
}
if (s === '') {
return valid;
}
for (let value of s) {
let v = value;
if (['{', '(', '['].indexOf(v) != -1) {
stack.push(v)
} else {
if (stack.length == 0) {
valid = false;
} else {
const va = stack.pop()
if (mapper[va] != v) {
valid = false;
}
}
}
}
return valid;
}
LeetCode20:validParentheses的更多相关文章
- [LeetCode]20.有效的括号(Java)
原题地址: valid-parentheses 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类 ...
- java web 开发三剑客 -------电子书
Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...
- 所有selenium相关的库
通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...
- LeetCode通关:栈和队列六连,匹配问题有绝招
刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...
- No.020:Valid Parentheses
问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- [LeetCode]题解(python):020-Valid Parentheses
题目来源: https://leetcode.com/problems/valid-parentheses/ 题意分析: 这道题输入一段只包括括号的字符串,判断这个字符串是否已经配对.配对的规则是,每 ...
- leetcode20
public class Solution { Stack<char> S = new Stack<char>(); public bool IsValid(string s) ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode题解】20_有效的括号(Valid-Parentheses)
目录 20_有效的括号(Valid-Parentheses) 描述 解法 思路 Java 实现 Python 实现 复杂度分析 20_有效的括号(Valid-Parentheses) 描述 给定一个只 ...
随机推荐
- java.sql.SQLException: Field 'id' doesn't have a default value
1:id 列要设置成自增,自动赋值 java.sql.SQLException: Field 'id' doesn't have a default value at com.mysql.jdbc.S ...
- Windows Server 2012 配置远程桌面帐户允许多用户同时登录
网上找了很多关于设置远程桌面最大连接数的文章,大都是说先要到控制面板的管理工具中设置远程桌面会话主机等,大体和我之前的文章<设置WINDOWS SERVER 2008修改远程桌面连接数>里 ...
- -bash: /etc/profile: line 11: syntax error near unexpected token `$'{\r''报错问题解决
在Linux系统配置Java环境变量之后执行 source /etc/profile指令报:-bash: /etc/profile: line 11: syntax error near unexpe ...
- 编译安装php5.6
sudo yum -y install curl-devel bzip2 bzip2-devel libxml2 libxml2-devel libjpeg libpng freetype li ...
- vs2015 行数统计
ctrol+shift+f 正則查找 b*[^:b#/]+.$
- Spring-bean的自动装配
bean的自动装配:可以让一个bean对象自动的引用其他bean byType:按照类型进行装配. 缺点:如果在IOC容器中存在多个类型相同的bean的时候,会出现异常. <bean id=& ...
- ELK的文档搭建
一.安装elasticsearch 官网:https://www.elastic.co/guide/index.html https://www.elastic.co/guide/en/elastic ...
- Django之发送邮件
Django的发送邮件是基于django的一个组件进行操作的,EmailMessage 基本使用方法: def send_html_mail(subject, html_content, from_a ...
- Document.write和 getElementById(ID)
在javascript中,document.write()方法:常用来网页向文档中输出内容. 示例:通过document.write()方法,向网页文档中输出了一段文字. document.write ...
- 1030 Travel Plan Dijkstra+dfs
和1018思路如出一辙,先求最短路径,再dfs遍历 #include <iostream> #include <cstdio> #include <vector> ...