Skip to content

Commit 4849be7

Browse files
committed
feat: add 020
1 parent e76aee8 commit 4849be7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
|13|[Roman to Integer][013]|Math, String|
1414
|14|[Longest Common Prefix][014]|String|
1515
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
16+
|20|[Valid Parentheses][020]|Stack, String|
1617

1718

1819
## Medium
@@ -40,3 +41,4 @@
4041
[013]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/013/README.md
4142
[014]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/014/README.md
4243
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
44+
[020]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/020/README.md

note/020/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [Valid Parentheses][title]
2+
3+
## Description
4+
5+
Given a string containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
6+
7+
The brackets must close in the correct order, `"()"` and `"()[]{}"` are all valid but `"(]"` and `"([)]"` are not.
8+
9+
**Tags:** Stack, String
10+
11+
## 思路
12+
13+
题意是判断括号匹配是否正确,很明显,我们可以用栈来解决这个问题,当出现左括号的时候入栈,当遇到右括号时,判断栈顶的左括号是否何其匹配,不匹配的话直接返回`false`即可,最终判断是否空栈即可,这里我们可以用数组模拟栈的操作使其操作更快,有个细节注意下`top = 1;`,从而省去了之后判空的操作和`top - 1`导致数组越界的错误。
14+
15+
``` java
16+
public class Solution {
17+
public boolean isValid(String s) {
18+
int len = s.length();
19+
char[] stack = new char[len + 1];
20+
int top = 1;
21+
for (int i = 0; i < len; ++i) {
22+
char c = s.charAt(i);
23+
if (c == '(' || c == '[' || c == '{')
24+
stack[top++] = c;
25+
else if (c == ')' && stack[top - 1] != '(')
26+
return false;
27+
else if (c == ']' && stack[top - 1] != '[')
28+
return false;
29+
else if (c == '}' && stack[top - 1] != '{')
30+
return false;
31+
else
32+
--top;
33+
}
34+
return top == 1;
35+
}
36+
}
37+
```
38+
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
43+
44+
45+
46+
[title]: https://leetcode.com/problems/valid-parentheses
47+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.blankj.easy._020;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/04/21
11+
* desc :
12+
* </pre>
13+
*/
14+
15+
public class Solution {
16+
public boolean isValid(String s) {
17+
int len = s.length();
18+
char[] stack = new char[len + 1];
19+
int top = 1;
20+
for (int i = 0; i < len; ++i) {
21+
char c = s.charAt(i);
22+
if (c == '(' || c == '[' || c == '{')
23+
stack[top++] = c;
24+
else if (c == ')' && stack[top - 1] != '(')
25+
return false;
26+
else if (c == ']' && stack[top - 1] != '[')
27+
return false;
28+
else if (c == '}' && stack[top - 1] != '{')
29+
return false;
30+
else
31+
--top;
32+
}
33+
return top == 1;
34+
}
35+
36+
public static void main(String[] args) {
37+
Solution solution = new Solution();
38+
}
39+
}

0 commit comments

Comments
 (0)