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) 描述 给定一个只 ...
随机推荐
- PAT A1010.Radix 二分法
PAT A1010.Radix 链接: https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 算法 ...
- Idea如果添加Maven模块
1.要创建一个和heaton-app同级的Maven模块,如果所示 2.点击下一步,添加ArtifactId,其中 groupId : 定义了项目属于哪个组,举个例子,如果你的公司是myc ...
- list常用方法
1.切片: ①.顾头不顾尾,从头开始取,但不包括最后一个. ②.从左向右数为正,从零开始,从右开始为负,从-1开始 如: names=['1','2','3'] ames[-1]与names[2]效果 ...
- windows 子系统 linux wsl 开启ssh 服务
原因 windows自带的终端操作丑陋,习惯xshell的操作 步骤 1.关闭windows自带的ssh服务,这个占用了22端口 2.wsl 安装ssh服务(使用的是ubuntu) sudo apt ...
- ABAP 图形练习(GFW_PRES_SHOW and GRAPH_2D)
创建屏幕0100(元素清单中含定制控制CONTAINER和OK_CODE) 创建GUI状态100(功能键含BACK和EXIT用于返回和退出 ) 代码 *&------------------- ...
- python生成exe文件
安装pyinstaller pyinstaller支持python2和python3 命令行安装:pip install pyinstaller pyinstaller --icon=duoguan. ...
- spring BeanWrapperImpl方便的嵌套属性(list)操作
beans 包主要提供了接口和类用于处理java beans. 其中最主要的接口是BeanWrapper: Spring 的中心接口,用于访问javabeans 的低层操作.默认实现为 ...
- vim配置文件.vimrc
20171127备份 syntax on "自动语法高亮 set number "显示行号 set autoindent "回车后自动缩进 set tabstop=4 & ...
- 起步:Proteus 8 仿真 Arduino 1.8.2
一.环境准备 1.从Arduino官网或中文社区下载并安装 Arduino IDE 当前最新版1.8.2:http://www.arduino.cn/thread-5838-1-1.html 2.下载 ...
- @Slf4j注解实现日志输出
自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTest.class); 每次写新的类,就需要重新 ...