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的更多相关文章

  1. [LeetCode]20.有效的括号(Java)

    原题地址: valid-parentheses 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类 ...

  2. java web 开发三剑客 -------电子书

    Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...

  3. 所有selenium相关的库

    通过爬虫 获取 官方文档库 如果想获取 相应的库 修改对应配置即可 代码如下 from urllib.parse import urljoin import requests from lxml im ...

  4. LeetCode通关:栈和队列六连,匹配问题有绝招

    刷题路线参考: https://github.com/chefyuan/algorithm-base https://github.com/youngyangyang04/leetcode-maste ...

  5. No.020:Valid Parentheses

    问题: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  6. [LeetCode]题解(python):020-Valid Parentheses

    题目来源: https://leetcode.com/problems/valid-parentheses/ 题意分析: 这道题输入一段只包括括号的字符串,判断这个字符串是否已经配对.配对的规则是,每 ...

  7. leetcode20

    public class Solution { Stack<char> S = new Stack<char>(); public bool IsValid(string s) ...

  8. 【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 ...

  9. 【LeetCode题解】20_有效的括号(Valid-Parentheses)

    目录 20_有效的括号(Valid-Parentheses) 描述 解法 思路 Java 实现 Python 实现 复杂度分析 20_有效的括号(Valid-Parentheses) 描述 给定一个只 ...

随机推荐

  1. 桌面小部件Wight父类AppWidgetProvider的三个方法

    onUpdate()这个方法会在每次更新App Widget的时候调用,数据更新的逻辑都写在这个方法里边.而且要注意的是:在用户添加小部件的时候,会首先调用这个方法,应该在这个方法里进行初始化操作,比 ...

  2. 【Java】基本数据类型

    基本知识点给个链接: https://blog.csdn.net/qwe969153746/article/details/53353534 问题: 1.3*0.1 == 0.3 返回什么: fals ...

  3. Python3.7 Scrapy crawl 运行出错解决方法

    安装的是Python3.7,装上依赖包和scrapy后运行爬虫命令出错 File "D:\Python37\lib\site-packages\scrapy\extensions\telne ...

  4. thinkpadE系列重装系统:u盘启动

    一.下载深度装机大师,制作启动u盘. 二.重启电脑:按F1;进入bios设置:     thinkpad e430c笔记本使用u盘装系统时无法使用u盘启动,这是由于thinkpad e430c笔记本u ...

  5. TJOI2010中位数

    中位数 上面是题目链接. 这一题比较水. 思路非常显然. 用mid查询时,只要返回中间值就行了. 主要就是add操作. 我们肯定不能插在末尾,然后用系统快排,这样只有30分. 那么正确的操作应该是二分 ...

  6. 4月23日 db 命令操作 和表操作

    1内容回顾: # 补充的知识点 # server端肯定是确定下来的 # mysql的客户端 # mysql.exe 直接在命令行就可以运行的 (学习阶段用) # navicat等可视化的客户端,是第三 ...

  7. 重写toFixed()方法

    1.直接在原型上修改,仍然使用元调用方式 Number.prototype.toFixed = function (exponent) { return parseInt(this * Math.po ...

  8. 更适用于JavaScript的设计模式:面向委托的设计,了解一下?(下)

    先来看一下传统的面向类式的写法: function Foo(name) { this.name = name; } Foo.prototype.sayName = function() { conso ...

  9. 实现PHP服务端和c#客户端数据交换

    服务端实现功能1,数据库的访问dbhelper.php包括执行语句返回多行,返回json数据,返回单条记录,返回第一行第一列的整数,返回第一行第一列的浮点数,返回第一行第一列的双精度数,返回第一行第一 ...

  10. 更改MAC地址,突破公司绑定MAC地址的限制

    步骤/方法 1 打开开始菜单,选择控制面板. 2   3 打开控制面板项,选择网络和共享中心. 4   5 选择更改适配器设置. 6   7 选择本地要修改MAC地址的网卡. 8   9 右键该网卡, ...