File tree Expand file tree Collapse file tree 4 files changed +72
-0
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 4 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -54,10 +54,32 @@ Note that an empty string is also considered valid.
54
54
*Output:* true
55
55
----
56
56
57
+ == 思路分析
57
58
58
59
[[src-0020]]
60
+ [tabs]
61
+ ====
62
+ 一刷::
63
+ +
64
+ --
59
65
[{java_src_attr}]
60
66
----
61
67
include::{sourcedir}/_0020_ValidParentheses.java[tag=answer]
62
68
----
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
+
63
85
Original file line number Diff line number Diff line change 474
474
|{doc_base_url} /0206-reverse-linked-list.adoc[题解]
475
475
|递给解法非常妙!
476
476
477
+ |{counter:codes}
478
+ |{leetcode_base_url} /valid-parentheses/[20. Valid Parentheses^]
479
+ |{doc_base_url} /0020-valid-parentheses.adoc[题解]
480
+ |栈
481
+
477
482
|===
478
483
479
484
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change @@ -62,6 +62,9 @@ public class _0020_ValidParentheses {
62
62
* Runtime: 2 ms, faster than 60.99% of Java online submissions for Valid Parentheses.
63
63
*
64
64
* 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
65
68
*/
66
69
public boolean isValid (String s ) {
67
70
if (Objects .isNull (s ) || s .length () == 0 ) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments