Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
9d44a444
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9d44a444
编写于
3月 14, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
validParenthese
上级
642b4d64
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
158 addition
and
0 deletion
+158
-0
README.md
README.md
+2
-0
assets/20.validParentheses.gif
assets/20.validParentheses.gif
+0
-0
validParentheses.md
validParentheses.md
+156
-0
未找到文件。
README.md
浏览文件 @
9d44a444
...
...
@@ -2,6 +2,8 @@
leetcode题解,记录自己的leecode解题之路。
## 传送门
### 简单难度
-
[
20. Valid Parentheses
](
https://github.com/azl397985856/leetcode/blob/master/validParentheses.md
)
### 中等难度
-
[
2. Add Two Numbers
](
https://github.com/azl397985856/leetcode/blob/master/addTwoNumbers.md
)
-
[
3. Longest Substring Without Repeating Characters
](
https://github.com/azl397985856/leetcode/blob/master/longestSubstringWithoutRepeatingCharacters.md
)
...
...
assets/20.validParentheses.gif
0 → 100644
浏览文件 @
9d44a444
207.6 KB
validParentheses.md
0 → 100644
浏览文件 @
9d44a444
## 题目地址
https://leetcode.com/problems/valid-parentheses/description
## 题目描述
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)如栈不为空且为对应的左半边括号,则取出栈顶元素,继续循环
2)若此时栈为空,则直接返回false
3)若不为对应的左半边括号,反之返回false
![
20.validParentheses
](
./assets/20.validParentheses.gif
)
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
## 关键点解析
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
])
{
valid
=
false
}
}
}
if
(
stack
.
length
>
0
)
return
false
;
return
valid
;
};
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录