Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
50eaab9e
Unverified
Commit
50eaab9e
authored
Jul 29, 2020
by
Zong
Committed by
GitHub
Jul 29, 2020
Browse files
Update LeetCode第20号问题:有效的括号.md
添加Java、python版代码实现
parent
3daa1877
Changes
1
Hide whitespace changes
Inline
Side-by-side
notes/LeetCode第20号问题:有效的括号.md
View file @
50eaab9e
...
...
@@ -71,7 +71,8 @@
### 代码实现
```
#### C++
```
c++
class
Solution
{
public:
bool
isValid
(
string
s
)
{
...
...
@@ -109,7 +110,52 @@ public:
}
};
```
#### Java
```
java
class
Solution
{
public
boolean
isValid
(
String
s
)
{
//String open="({[";
//String close="]})";
Stack
<
Character
>
stack
=
new
Stack
<
Character
>();
if
(
s
.
length
()
%
2
!=
0
)
return
false
;
for
(
char
c
:
s
.
toCharArray
())
{
if
(
c
==
'('
)
stack
.
push
(
')'
);
else
if
(
c
==
'{'
)
stack
.
push
(
'}'
);
else
if
(
c
==
'['
)
stack
.
push
(
']'
);
else
if
(
stack
.
isEmpty
()
||
stack
.
pop
()
!=
c
)
return
false
;
}
return
stack
.
isEmpty
();
}
}
```
#### Python
```
python
class
Solution
(
object
):
def
isValid
(
self
,
s
):
open_list
=
[
"["
,
"{"
,
"("
]
close_list
=
[
"]"
,
"}"
,
")"
]
stack
=
[]
for
i
in
s
:
if
i
in
open_list
:
stack
.
append
(
i
)
elif
i
in
close_list
:
pos
=
close_list
.
index
(
i
)
if
len
(
stack
)
>
0
and
(
open_list
[
pos
]
==
stack
[
len
(
stack
)
-
1
]):
stack
.
pop
()
else
:
return
False
if
len
(
stack
)
==
0
:
return
True
```

\ No newline at end of file

Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment