20.validParentheses.md 3.4 KB
Newer Older
L
luzhipeng 已提交
1 2 3 4
## 题目地址
https://leetcode.com/problems/valid-parentheses/description

## 题目描述
L
luzhipeng 已提交
5 6

```
L
luzhipeng 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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
L
luzhipeng 已提交
35
```
L
luzhipeng 已提交
36 37 38

## 思路

L
luzhipeng 已提交
39 40
关于这道题的思路,邓俊辉讲的非常好,没有看过的同学可以看一下, [视频地址](http://www.xuetangx.com/courses/course-v1:TsinghuaX+30240184+sp/courseware/ad1a23c053df4501a3facd66ef6ccfa9/8d6f450e7f7a445098ae1d507fda80f6/)

L
luzhipeng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
使用栈,遍历输入字符串

如果当前字符为左半边括号时,则将其压入栈中

如果遇到右半边括号时,分类讨论:

1)如栈不为空且为对应的左半边括号,则取出栈顶元素,继续循环  

2)若此时栈为空,则直接返回false

3)若不为对应的左半边括号,反之返回false



L
luzhipeng 已提交
55
![20.validParentheses](../assets/20.validParentheses.gif)
L
luzhipeng 已提交
56 57 58

(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)

L
luzhipeng 已提交
59 60 61 62 63
> 值得注意的是,如果题目要求只有一种括号,那么我们其实可以使用更简洁,更省内存的方式 - 计数器来进行求解,而
不必要使用栈。

> 事实上,这类问题还可以进一步扩展,我们可以去解析类似HTML等标记语法, 比如 <p></p> <body></body>

L
luzhipeng 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
## 关键点解析

1. 栈的基本特点和操作
2. 如果你用的是JS没有现成的栈,可以用数组来模拟
入: push  出:  pop

> 入: push  出 shift 就是队列
## 代码


```js
/*
 * @lc app=leetcode id=20 lang=javascript
 *
 * [20] Valid Parentheses
 *
 * https://leetcode.com/problems/valid-parentheses/description/
 *
 * algorithms
 * Easy (35.97%)
 * Total Accepted:    530.2K
 * Total Submissions: 1.5M
 * Testcase Example:  '"()"'
 *
 * 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
 * 
 * 
 */
/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    let valid = true;
    const stack = [];
    const mapper = {
        '{': "}",
        "[": "]",
        "(": ")"
    }
    
    for(let i in s) {
        const v = s[i];
        if (['(', '[', '{'].indexOf(v) > -1) {
            stack.push(v);
        } else {
            const peak = stack.pop();
            if (v !== mapper[peak]) {
L
luzhipeng 已提交
156
                return false;
L
luzhipeng 已提交
157 158 159 160 161 162 163 164
            }
        }
    }

    if (stack.length > 0) return false;

    return valid;
};
L
luzhipeng 已提交
165 166 167 168
```

## 扩展
如果让你检查XML标签是否闭合如何检查, 更进一步如果要你实现一个简单的XML的解析器,应该怎么实现?