Skip to content

Commit 71444ee

Browse files
committed
二刷20
1 parent 2d742c2 commit 71444ee

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

docs/0020-valid-parentheses.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,32 @@ Note that an empty string is also considered valid.
5454
*Output:* true
5555
----
5656

57+
== 思路分析
5758

5859
[[src-0020]]
60+
[tabs]
61+
====
62+
一刷::
63+
+
64+
--
5965
[{java_src_attr}]
6066
----
6167
include::{sourcedir}/_0020_ValidParentheses.java[tag=answer]
6268
----
69+
--
70+
71+
二刷::
72+
+
73+
--
74+
[{java_src_attr}]
75+
----
76+
include::{sourcedir}/_0020_ValidParentheses_2.java[tag=answer]
77+
----
78+
--
79+
====
80+
81+
== 参考资料
82+
83+
. https://leetcode.cn/problems/valid-parentheses/solutions/373578/you-xiao-de-gua-hao-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[20. 有效的括号 - 官方题解^]
84+
6385

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@
474474
|{doc_base_url}/0206-reverse-linked-list.adoc[题解]
475475
|递给解法非常妙!
476476

477+
|{counter:codes}
478+
|{leetcode_base_url}/valid-parentheses/[20. Valid Parentheses^]
479+
|{doc_base_url}/0020-valid-parentheses.adoc[题解]
480+
|栈
481+
477482
|===
478483

479484
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0020_ValidParentheses.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class _0020_ValidParentheses {
6262
* Runtime: 2 ms, faster than 60.99% of Java online submissions for Valid Parentheses.
6363
*
6464
* Memory Usage: 34.2 MB, less than 100.00% of Java online submissions for Valid Parentheses.
65+
*
66+
* @author D瓜哥 · https://www.diguage.com
67+
* @since 2019-07-26 08:12
6568
*/
6669
public boolean isValid(String s) {
6770
if (Objects.isNull(s) || s.length() == 0) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
public class _0020_ValidParentheses_2 {
7+
// tag::answer[]
8+
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2024-09-13 16:51:17
12+
*/
13+
public boolean isValid(String s) {
14+
Deque<Character> stack = new LinkedList<>();
15+
for (int i = 0; i < s.length(); i++) {
16+
char c = s.charAt(i);
17+
if (c == '(' || c == '[' || c == '{') {
18+
stack.addLast(c);
19+
} else if (c == ')') {
20+
if (stack.isEmpty() || stack.peekLast() != '(') {
21+
return false;
22+
} else {
23+
stack.removeLast();
24+
}
25+
} else if (c == ']') {
26+
if (stack.isEmpty() || stack.peekLast() != '[') {
27+
return false;
28+
} else {
29+
stack.removeLast();
30+
}
31+
} else if (c == '}') {
32+
if (stack.isEmpty() || stack.peekLast() != '{') {
33+
return false;
34+
} else {
35+
stack.removeLast();
36+
}
37+
}
38+
}
39+
return stack.isEmpty();
40+
}
41+
// end::answer[]
42+
}

0 commit comments

Comments
 (0)