Skip to content

Commit e243224

Browse files
committed
20. Valid Parentheses
1 parent f45311d commit e243224

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

src/leetcode/_0_template/Main.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package leetcode._0_template;
2+
3+
import leetcode._19_.ListNode;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
System.out.println( solution.isValid(""));
12+
}
13+
}
14+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package leetcode._0_template;
2+
3+
class Solution {
4+
public boolean isValid(String s) {
5+
return true;
6+
}
7+
}

src/leetcode/_0_template/solution.md

Whitespace-only changes.

src/leetcode/_20_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._20_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.isValid("["));
10+
}
11+
}
12+

src/leetcode/_20_/Solution.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode._20_;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
class Solution {
8+
public boolean isValid(String s) {
9+
List<Character> left = Arrays.asList('(', '{', '[');
10+
List<Character> right = Arrays.asList(')', '}', ']');
11+
Stack<Integer> stack = new Stack<>();
12+
for (char c : s.toCharArray()) {
13+
if (left.contains(c)) {
14+
stack.push(left.indexOf(c));
15+
} else if (right.contains(c)) {
16+
if (stack.empty() || !stack.pop().equals(right.indexOf(c))) {
17+
return false;
18+
}
19+
} else {
20+
return false;
21+
}
22+
}
23+
return stack.empty();
24+
}
25+
}

src/leetcode/_20_/solution.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### [20\. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a string containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
7+
8+
An input string is valid if:
9+
10+
1. Open brackets must be closed by the same type of brackets.
11+
2. Open brackets must be closed in the correct order.
12+
13+
Note that an empty string is also considered valid.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: "()"
19+
Output: true
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: "()[]{}"
26+
Output: true
27+
```
28+
29+
**Example 3:**
30+
31+
```
32+
Input: "(]"
33+
Output: false
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: "([)]"
40+
Output: false
41+
```
42+
43+
**Example 5:**
44+
45+
```
46+
Input: "{[]}"
47+
Output: true
48+
```
49+
50+
51+
#### Solution
52+
53+
Language: **Java**
54+
55+
```java
56+
class Solution {
57+
   public boolean isValid(String s) {
58+
       List<Character> left = Arrays.asList('(', '{', '[');
59+
       List<Character> right = Arrays.asList(')', '}', ']');
60+
       Stack<Integer> stack = new Stack<>();
61+
       for (char c : s.toCharArray()) {
62+
           if (left.contains(c)) {
63+
               stack.push(left.indexOf(c));
64+
          } else if (right.contains(c)) {
65+
               if (stack.empty() || !stack.pop().equals(right.indexOf(c))) {
66+
                   return false;
67+
              }
68+
          } else {
69+
               return false;
70+
          }
71+
      }
72+
       return stack.empty();
73+
  }
74+
}
75+
```
76+
![](https://ws3.sinaimg.cn/large/006tKfTcgy1g0uh8bio97j30yo0u0jvz.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy