|
| 1 | +## 题目地址 |
| 2 | +https://leetcode.com/problems/valid-parentheses/description |
| 3 | + |
| 4 | +## 题目描述 |
| 5 | +Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. |
| 6 | + |
| 7 | +An input string is valid if: |
| 8 | + |
| 9 | +Open brackets must be closed by the same type of brackets. |
| 10 | +Open brackets must be closed in the correct order. |
| 11 | +Note that an empty string is also considered valid. |
| 12 | + |
| 13 | +Example 1: |
| 14 | + |
| 15 | +Input: "()" |
| 16 | +Output: true |
| 17 | +Example 2: |
| 18 | + |
| 19 | +Input: "()[]{}" |
| 20 | +Output: true |
| 21 | +Example 3: |
| 22 | + |
| 23 | +Input: "(]" |
| 24 | +Output: false |
| 25 | +Example 4: |
| 26 | + |
| 27 | +Input: "([)]" |
| 28 | +Output: false |
| 29 | +Example 5: |
| 30 | + |
| 31 | +Input: "{[]}" |
| 32 | +Output: true |
| 33 | + |
| 34 | + |
| 35 | +## 思路 |
| 36 | + |
| 37 | +使用栈,遍历输入字符串 |
| 38 | + |
| 39 | +如果当前字符为左半边括号时,则将其压入栈中 |
| 40 | + |
| 41 | +如果遇到右半边括号时,分类讨论: |
| 42 | + |
| 43 | +1)如栈不为空且为对应的左半边括号,则取出栈顶元素,继续循环 |
| 44 | + |
| 45 | +2)若此时栈为空,则直接返回false |
| 46 | + |
| 47 | +3)若不为对应的左半边括号,反之返回false |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +(图片来自: https://github.com/MisterBooo/LeetCodeAnimation) |
| 54 | + |
| 55 | +## 关键点解析 |
| 56 | + |
| 57 | +1. 栈的基本特点和操作 |
| 58 | +2. 如果你用的是JS没有现成的栈,可以用数组来模拟 |
| 59 | +入: push 出: pop |
| 60 | + |
| 61 | +> 入: push 出 shift 就是队列 |
| 62 | +## 代码 |
| 63 | + |
| 64 | + |
| 65 | +```js |
| 66 | +/* |
| 67 | + * @lc app=leetcode id=20 lang=javascript |
| 68 | + * |
| 69 | + * [20] Valid Parentheses |
| 70 | + * |
| 71 | + * https://leetcode.com/problems/valid-parentheses/description/ |
| 72 | + * |
| 73 | + * algorithms |
| 74 | + * Easy (35.97%) |
| 75 | + * Total Accepted: 530.2K |
| 76 | + * Total Submissions: 1.5M |
| 77 | + * Testcase Example: '"()"' |
| 78 | + * |
| 79 | + * Given a string containing just the characters '(', ')', '{', '}', '[' and |
| 80 | + * ']', determine if the input string is valid. |
| 81 | + * |
| 82 | + * An input string is valid if: |
| 83 | + * |
| 84 | + * |
| 85 | + * Open brackets must be closed by the same type of brackets. |
| 86 | + * Open brackets must be closed in the correct order. |
| 87 | + * |
| 88 | + * |
| 89 | + * Note that an empty string is also considered valid. |
| 90 | + * |
| 91 | + * Example 1: |
| 92 | + * |
| 93 | + * |
| 94 | + * Input: "()" |
| 95 | + * Output: true |
| 96 | + * |
| 97 | + * |
| 98 | + * Example 2: |
| 99 | + * |
| 100 | + * |
| 101 | + * Input: "()[]{}" |
| 102 | + * Output: true |
| 103 | + * |
| 104 | + * |
| 105 | + * Example 3: |
| 106 | + * |
| 107 | + * |
| 108 | + * Input: "(]" |
| 109 | + * Output: false |
| 110 | + * |
| 111 | + * |
| 112 | + * Example 4: |
| 113 | + * |
| 114 | + * |
| 115 | + * Input: "([)]" |
| 116 | + * Output: false |
| 117 | + * |
| 118 | + * |
| 119 | + * Example 5: |
| 120 | + * |
| 121 | + * |
| 122 | + * Input: "{[]}" |
| 123 | + * Output: true |
| 124 | + * |
| 125 | + * |
| 126 | + */ |
| 127 | +/** |
| 128 | + * @param {string} s |
| 129 | + * @return {boolean} |
| 130 | + */ |
| 131 | +var isValid = function(s) { |
| 132 | + let valid = true; |
| 133 | + const stack = []; |
| 134 | + const mapper = { |
| 135 | + '{': "}", |
| 136 | + "[": "]", |
| 137 | + "(": ")" |
| 138 | + } |
| 139 | + |
| 140 | + for(let i in s) { |
| 141 | + const v = s[i]; |
| 142 | + if (['(', '[', '{'].indexOf(v) > -1) { |
| 143 | + stack.push(v); |
| 144 | + } else { |
| 145 | + const peak = stack.pop(); |
| 146 | + if (v !== mapper[peak]) { |
| 147 | + valid = false |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (stack.length > 0) return false; |
| 153 | + |
| 154 | + return valid; |
| 155 | +}; |
| 156 | +``` |
0 commit comments