-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #907 from forest000014/main
[forest000014] Week 6
- Loading branch information
Showing
5 changed files
with
376 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
solution 1. brute force | ||
Time Complexity: O(n^2) | ||
Space Complexity: O(1) | ||
๋ชจ๋ ์ ๋ถ i์ ๋ํด, ๊ฐ์ฅ ๋ง์ ๋ฌผ์ ๋ด์ ์ ์๋ ๋ฐ๋์ชฝ ์ ๋ถ j๋ฅผ ์ฐพ๋๋ค. | ||
solution 2. PQ | ||
Time Complexity: O(nlogn) | ||
- ์ ๋ ฌ : O(nlogn) | ||
- i๋ฒ์งธ ์ ๋ถ์ ๋ํด (์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์) ๊ฐ์ฅ ๋ฉ๋ฆฌ์๋ ์ ๋ถ ํ์(O(logn)) * n = O(nlogn) | ||
Space Complexity: O(n) | ||
(์ฌ๊ณ ์ ํ๋ฆ์ ์ ๊ธฐ ์ํด ํธ์์ ๋ฐ๋ง๋ก ์ ์์ต๋๋ค... ^^) | ||
brute force ์์ ๋ถํ์ํ ํ์์ ์ค์ฌ๋ณด์. | ||
์ผ๋จ ์ด๋ค ์ ๋ถ i๊ฐ ์ฃผ์ด์ก๋ค๊ณ ๊ฐ์ ํ์. i๋ฅผ ํ์ชฝ ๋ฒฝ์ผ๋ก ํด์ ๊ฐ์ฅ ๋ง์ ๋ฌผ์ ๋ด๋ ๋ฐฉ๋ฒ์ ์ฐพ์ผ๋ ค๋ฉด, ๋ฐ๋์ ๋๋จธ์ง ๋ชจ๋ ์ ๋ถ์ ํ์ํด์ผ๋ง ํ ๊น? | ||
(1) ์์ ๋ณด๋ค ์์ ์ ๋ถ์ ํ์ํ์ง ์๋๋ค. | ||
์์ ๋ณด๋ค ํฐ ์ ๋ถ๋ง์ ํ์ํด๋ ์ถฉ๋ถํ๋ค. | ||
์๋ํ๋ฉด, ์ค๋ น ์์ ๋ณด๋ค ๋ ์์ ์ ๋ถ ์ค์ ์ ๋ต์ด ์์๋ค๊ณ ํ๋๋ผ๋, ๊ทธ ์ ๋ถ์ ๊ธฐ์ค์ผ๋ก ํ์ํ ๋ ์ ๋ต์ ํด๋นํ๋ ์์ ํ์ธํ๊ฒ ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ด๋ค. | ||
(2) ์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ์ ๋ถ๋ง์ ํ์ ๋์์ผ๋ก ์ผ๋๋ค๋ฉด, ๊ฐ์ฅ ๋ฉ๋ฆฌ์๋ ์ ๋ถ๋ง ํ์ธํ๋ฉด ๋๋ค. | ||
ํ์ ๋์์ด ์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ์ ๋ถ๋ค์ด๋ผ๋ฉด, ์ด์ฐจํผ ๋ด์ ์ ์๋ ๋ฌผ์ ๋์ด๋ ์์ ์ ๋์ด๋ก ์ผ์ ํ๋ค. | ||
๋ฐ๋ผ์, ๋ฉ๋ฆฌ์๋ ์ ๋ถ์ผ์๋ก ๋ ๋ง์ ๋ฌผ์ ๋ด๊ฒ ๋๋ค. | ||
์ฆ, "์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด์", "๊ฐ์ฅ ๋ฉ๋ฆฌ ์๋(์ค๋ฅธ์ชฝ์ด๋ ์ผ์ชฝ์ด๋ )" ์ ๋ถ๋ง์ ํ๋ณด๋ก ์ผ์ ํ์ธํ๋ฉด ์ถฉ๋ถํ๋ค. | ||
(๊ทธ ์ธ์ ๋๋จธ์ง, ์ฆ ์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด์, ๊ฐ์ฅ ๋ฉ๋ฆฌ์์ง ์์ ๋๋จธ์ง ์ ๋ถ๋ค์, ์์ฐ์ค๋ฝ๊ฒ ํ์ํ์ง ์์ ์ ์๋ค.) | ||
์ ๋ฆฌํ์๋ฉด, ์ฃผ์ด์ง ์ ๋ถ i์ ๋ํด์, ๋จ 2๋ฒ(์ค๋ฅธ์ชฝ, ์ผ์ชฝ)์ ํ์๋ง ํ๋ฉด ์ถฉ๋ถํ๋ค. | ||
(3) ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๊ณผ 2๊ฐ์ PQ๋ฅผ ์๋์ฒ๋ผ ํ์ฉํ๋ฉด, ์ ํ์์ O(logn) ์๊ฐ์ ์ํํ ์ ์๋ค. | ||
PQ๋ ๊ฐ๊ฐ x ์ขํ ๊ธฐ์ค max heap(๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์ ๋ถ์ ์ฐพ๊ธฐ ์ํจ), x ์ขํ ๊ธฐ์ค min heap(๊ฐ์ฅ ์ผ์ชฝ์ ์ ๋ถ์ ์ฐพ๊ธฐ ์ํจ)์ ์ฌ์ฉํ๋ค. | ||
์ ๋ถ๋ค์ ๊ธธ์ด ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํด๋๊ณ , ํ๋์ฉ ์ํํ๋ฉด์ (say, i๋ฒ์งธ ์ ๋ถ), ์๋ ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค. | ||
- ์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ผ๋ก ๋ฉ๋ฆฌ ์๋ ์ ๋ถ์ ์์น๋ฅผ ์ฐพ์์(= ํ์ฌ PQ(max heap)์ root), ์ต๋ ๋ฌผ์ ์์ ๊ณ์ฐํ๋ค. | ||
- ์์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด์ ๊ฐ์ฅ ์ผ์ชฝ์ผ๋ก ๋ฉ๋ฆฌ ์๋ ์ ๋ถ์ ์์น๋ฅผ ์ฐพ์์(= ํ์ฌ PQ(min heap)์ root), ์ต๋ ๋ฌผ์ ์์ ๊ณ์ฐํ๋ค. | ||
- i๋ฒ์งธ ์ ๋ถ์ PQ 2๊ฐ์ ๊ฐ๊ฐ ๋ฃ๋๋ค. | ||
solution 3. two pointers | ||
(AlgoDale ํ์ด๋ฅผ ์ฐธ๊ณ ํจ) | ||
Time Complexity: O(n) | ||
Space Complexity: O(1) | ||
2 ํฌ์ธํฐ๋ฅผ ํ์ฉํ๋ฉด, PQ๋ ์์ด ์๊ฐ ๋ณต์ก๋๋ฅผ O(n)์ผ๋ก ์ค์ผ ์ ์์๋ค. | ||
๋จ์ํ "ํฐ ์ชฝ์ ์ค์ด๊ธฐ๋ณด๋ค๋, ์์ ์ชฝ์ ์ค์ด๋ ๊ฒ ์ ๋ฆฌํ๊ฒ ์ง" ์ ๋์ greedyํ ๋ ผ๋ฆฌ๋ ์ถฉ๋ถํ์ง ์์ ๊ฒ ๊ฐ๊ณ , ๋ ๋ช ํํ ๊ทผ๊ฑฐ๊ฐ ์์ ๊ฒ ๊ฐ์๋ฐ ์๊ฐ ๊ด๊ณ์ ๊ณ ๋ฏผํด๋ณด์ง๋ ๋ชปํ๋ค. | ||
To-Do : ํ์ด๊ฐ ๋๊ฐ ์ดํด๋ ๋์์ง๋ง, ์ด๊ฒ ์ ๋๋์ง, ์๋ฐํ๊ฒ ์ดํดํ๊ธฐ ์ํด PQ๋ฅผ ์ฌ์ฉํ๋ ๋ ผ๋ฆฌ๋ฅผ ์ข ๋ ๋ฐ์ ์์ผ๋ณผ ํ์๊ฐ ์๋ค. | ||
*/ | ||
class Solution { | ||
public int maxArea(int[] height) { | ||
int i = 0, j = height.length - 1; | ||
|
||
int ans = 0; | ||
while (i < j) { | ||
int area = (j - i) * Math.min(height[i], height[j]); | ||
if (area > ans) { | ||
ans = area; | ||
} | ||
|
||
if (height[i] <= height[j]) { | ||
i++; | ||
} else { | ||
j--; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
////// ์๋๋ solution 2 | ||
private static class Tuple { | ||
private int x; | ||
private int h; | ||
|
||
Tuple(int x, int h) { | ||
this.x = x; | ||
this.h = h; | ||
} | ||
} | ||
|
||
public int maxArea2(int[] height) { | ||
List<Tuple> tuples = IntStream.range(0, height.length) | ||
.mapToObj(i -> new Tuple(i, height[i])) | ||
.collect(Collectors.toList()); | ||
Collections.sort(tuples, (a, b) -> b.h - a.h); | ||
|
||
PriorityQueue<Tuple> minPq = new PriorityQueue<>((a, b) -> a.x - b.x); | ||
PriorityQueue<Tuple> maxPq = new PriorityQueue<>((a, b) -> b.x - a.x); | ||
|
||
int ans = 0; | ||
for (int i = 0; i < height.length; i++) { | ||
minPq.add(tuples.get(i)); | ||
maxPq.add(tuples.get(i)); | ||
|
||
Tuple curr = tuples.get(i); | ||
|
||
Tuple left = minPq.peek(); | ||
int leftArea = (curr.x - left.x) * curr.h; | ||
if (leftArea > ans) { | ||
ans = leftArea; | ||
} | ||
|
||
Tuple right = maxPq.peek(); | ||
int rightArea = (right.x - curr.x) * curr.h; | ||
if (rightArea > ans) { | ||
ans = rightArea; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
design-add-and-search-words-data-structure/forest000014.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
Time Complexity: | ||
- add: O(w) | ||
- search: O(26^2 * w) = O(w) | ||
Space Complexity: O(w) | ||
Trie๋ฅผ ํ์ฉํ๋, '.'์ ํ์์ด ํ์ํ ๊ฒฝ์ฐ์๋ for๋ฌธ์ ์ฌ์ฉํ๋ค. | ||
*/ | ||
class WordDictionary { | ||
class Node { | ||
public char ch; | ||
public boolean ends; | ||
public Map<Character, Node> children; | ||
|
||
Node() { | ||
this.children = new HashMap<>(); | ||
} | ||
|
||
Node(char ch) { | ||
this.ch = ch; | ||
this.children = new HashMap<>(); | ||
} | ||
} | ||
|
||
Node root; | ||
|
||
public WordDictionary() { | ||
this.root = new Node(); | ||
} | ||
|
||
public void addWord(String word) { | ||
Node curr = this.root; | ||
|
||
for (int i = 0; i < word.length(); i++) { | ||
char ch = word.charAt(i); | ||
if (!curr.children.containsKey(ch)) { | ||
curr.children.put(ch, new Node(ch)); | ||
} | ||
|
||
curr = curr.children.get(ch); | ||
} | ||
|
||
curr.ends = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
return searchChar(word, 0, this.root); | ||
} | ||
|
||
private boolean searchChar(String word, int idx, Node curr) { | ||
if (curr == null) { | ||
return false; | ||
} else if (idx == word.length()) { | ||
return curr.ends; | ||
} | ||
|
||
char ch = word.charAt(idx); | ||
|
||
if (ch == '.') { | ||
for (Character key : curr.children.keySet()) { | ||
if (searchChar(word, idx + 1, curr.children.get(key))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} else { | ||
return searchChar(word, idx + 1, curr.children.get(ch)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Your WordDictionary object will be instantiated and called as such: | ||
* WordDictionary obj = new WordDictionary(); | ||
* obj.addWord(word); | ||
* boolean param_2 = obj.search(word); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
# Time Complexity: O(nlogn) | ||
- tuples ArrayList ์ ๋ ฌ: O(nlogn) | ||
- ์ธ๋ฑ์ค ํธ๋ฆฌ ์ฝ์ : O(logn) * n times = O(nlogn) | ||
- ์ธ๋ฑ์ค ํธ๋ฆฌ ์กฐํ: O(logn) * n times = O(nlogn) | ||
# Space Complexity: O(nlogn) | ||
- tuples ArrayList: O(n) | ||
- ์ธ๋ฑ์ค ํธ๋ฆฌ: O(nlogn) | ||
# solution | ||
์ด ๋ฌธ์ ๋ DP๋ก ์ ๊ทผํ์ต๋๋ค. | ||
LIS(1, x)๋ฅผ ๋ฒ์ [1, x] ๋ด์ LIS(๋จ, nums[x]๋ฅผ ๋ฐ๋์ ํฌํจ)์ ๊ธธ์ด๋ผ๊ณ ์ ์ํ๊ฒ ์ต๋๋ค. - (1) | ||
1 <= j < i ์ธ ๋ชจ๋ j์ ํ LIS(1, j)๋ฅผ ์๊ณ ์๋ค๋ฉด, LIS(1, i)๋ ์๋์ ๊ฐ์ด ๊ตฌํ ์ ์์ต๋๋ค. | ||
LIS(1, i) = max(LIS(1, j)) (๋จ, j๋ 1 <= j < i ์ด๊ณ , nums[j] < nums[i]) - (2) | ||
max(LIS(1, j))๋ฅผ ๊ตฌํ ๋, ๋ชจ๋ j์ ๋ํด ํ์ํ๋ค๋ฉด, ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(n^2)๊ฐ ๋๊ธฐ ๋๋ฌธ์, ์๊ฐ ๋ณต์ก๋๋ฅผ ์ค์ผ ํ์๊ฐ ์์ต๋๋ค. | ||
์ด ํ์ ๊ณผ์ ์ ์ค์ด๊ธฐ ์ํด, ์๋์ ์ฌ๊ณ ๊ณผ์ ์ ๊ฑฐ์ณค์ต๋๋ค. | ||
์ด๋ค ๋ฒ์ ๋ด์ ๊ฐ์ฅ ํฐ ๊ฐ์ O(logn) ์๊ฐ์ ๊ตฌํ๊ธฐ ์ํ ์๋ฃ๊ตฌ์กฐ๋ก, ์ธ๋ฑ์ค ํธ๋ฆฌ(ํน์ ์ธ๊ทธ๋จผํธ ํธ๋ฆฌ)๋ฅผ ์ฌ์ฉํฉ๋๋ค. | ||
(์ด ์ธ๋ฑ์ค ํธ๋ฆฌ์ x๋ฒ์งธ leaf ๋ ธ๋์๋ LIS(1, x) ๊ฐ์ ์ ์ฅํ๊ณ , internal ๋ ธ๋์๋ ์์ ๋ ธ๋๋ค ์ค ๊ฐ์ฅ ํฐ ๊ฐ์ ์ ์ฅํฉ๋๋ค.) | ||
๋ค๋ง, ๋จ์ํ ํด๋น ๋ฒ์ ๋ด์ ๊ฐ์ฅ ํฐ ๊ฐ์ ๊ตฌํ๋ ๊ฒ๋ง์ผ๋ก๋ ๋ถ์กฑํ๊ณ , nums[j] < nums[i]์ธ j๋ง์ ํ๋ณด๋ก ์ผ์์ผ ํ ํ ๋ฐ์, | ||
๊ทธ๋ฌ๊ธฐ ์ํด์, ์ธ๋ฑ์ค ํธ๋ฆฌ์ ๋ชจ๋ leaf ๋ ธ๋๋ฅผ ๋ฏธ๋ฆฌ ์ฝ์ ํด๋๋ ๊ฒ์ด ์๋๋ผ ์๋์ฒ๋ผ ์์ฐจ์ ์ผ๋ก max(LIS(1, i))์ ๊ณ์ฐ๊ณผ ์ฝ์ ์ ๋ฒ๊ฐ์ ์ํํฉ๋๋ค. | ||
nums[i]์ ํฌ๊ธฐ๊ฐ ์์ ๊ฒ๋ถํฐ ์์๋๋ก, "max(LIS(1, j))๋ฅผ ๊ณ์ฐํ๊ณ , leaf๋ฅผ ํ๋ ์ฝ์ "ํ๋ ๊ณผ์ ์ ๋ฐ๋ณตํฉ๋๋ค. | ||
nums[i]๋ณด๋ค ๋ ํฐ ๊ฐ์ ์์ง ์ธ๋ฑ์ค ํธ๋ฆฌ์ ์ฝ์ ๋์ง ์์ ์ํ์ด๊ธฐ ๋๋ฌธ์, ์ธ๋ฑ์ค ํธ๋ฆฌ์์ ๊ตฌ๊ฐ [1, i-1]์ ์ต๋๊ฐ์ ์กฐํํ๋ฉด nums[j] < num[i]์ธ j์ ๋ํด์๋ง ์ต๋๊ฐ์ ์ฐพ๊ฒ ๋๋ฏ๋ก, | ||
(2)๋ฒ ๊ณผ์ ์ O(logn) ์๊ฐ์ ๊ตฌํ ์ ์์ต๋๋ค. | ||
๋ฐ๋ผ์ ์ ์ฒด ์๊ฐ ๋ณต์ก๋๋ O(nlogn)์ด ๋ฉ๋๋ค. | ||
*/ | ||
class Solution { | ||
|
||
int[] tree; | ||
int L = 1; | ||
|
||
public int lengthOfLIS(int[] nums) { | ||
init(nums); | ||
ArrayList<Tuple> tuples = new ArrayList<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
tuples.add(new Tuple(i, nums[i])); | ||
} | ||
|
||
Collections.sort(tuples, (a, b) -> { | ||
if (a.val == b.val) { | ||
return b.ref - a.ref; // 2์์ : ref ๋ด๋ฆผ์ฐจ์ | ||
} else { | ||
return a.val - b.val; // 1์์ : val ์ค๋ฆ์ฐจ์ | ||
} | ||
}); | ||
|
||
int ans = 0; | ||
for (int i = 0; i < nums.length; i++) { | ||
int curr = getMax(0, tuples.get(i).ref - 1) + 1; | ||
ans = Math.max(ans, curr); | ||
insert(tuples.get(i).ref, curr); | ||
} | ||
|
||
return ans; | ||
} | ||
|
||
public class Tuple { | ||
public int ref; | ||
public int val; | ||
|
||
public Tuple(int ref, int val) { | ||
this.ref = ref; | ||
this.val = val; | ||
} | ||
} | ||
|
||
public void init(int[] nums) { | ||
while (L < nums.length) { | ||
L *= 2; | ||
} | ||
|
||
tree = new int[L * 2]; | ||
|
||
for (int i = 1; i < L * 2; i++) { | ||
tree[i] = 0; | ||
} | ||
} | ||
|
||
public void insert(int idx, int v) { | ||
int i = idx + L; | ||
tree[i] = v; | ||
i /= 2; | ||
while (i >= 1) { | ||
tree[i] = Math.max(tree[i * 2], tree[i * 2 + 1]); | ||
i /= 2; | ||
} | ||
} | ||
|
||
public int getMax(int l, int r) { | ||
int i = l + L; | ||
int j = r + L; | ||
int ret = 0; | ||
while (i <= j) { | ||
if (i % 2 == 1) { | ||
ret = Math.max(ret, tree[i++]); | ||
} | ||
if (j % 2 == 0) { | ||
ret = Math.max(ret, tree[j--]); | ||
} | ||
i /= 2; | ||
j /= 2; | ||
} | ||
|
||
return ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Time Complexity: O(m * n) | ||
Space Complexity: O(1) | ||
ํ์ฌ ์์น๋ฅผ r, c๋ผ๋ ๋ณ์๋ฅผ ์ฌ์ฉํด์ ๋ํ๋ด๊ณ , ์ด r, c๋ฅผ ์ง์ ์ ์ดํ๋ ๋ฐฉ์์ผ๋ก ํ๋ ฌ์ ์ํํ๋ค. | ||
*/ | ||
class Solution { | ||
public List<Integer> spiralOrder(int[][] matrix) { | ||
List<Integer> ans = new ArrayList<>(); | ||
|
||
int m = matrix.length, n = matrix[0].length; | ||
int r = 0, c = 0; | ||
int[] dr = {0, 1, 0, -1}; | ||
int[] dc = {1, 0, -1, 0}; | ||
int d = 0; | ||
final int VISITED = -999; | ||
|
||
while (ans.size() < m * n) { | ||
ans.add(matrix[r][c]); | ||
matrix[r][c] = VISITED; | ||
|
||
int nr = r + dr[d]; | ||
int nc = c + dc[d]; | ||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || matrix[nr][nc] == VISITED) { | ||
d = (d + 1) % 4; | ||
nr = r + dr[d]; | ||
nc = c + dc[d]; | ||
} | ||
r = nr; | ||
c = nc; | ||
} | ||
|
||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Time Complexity: O(n) | ||
Space Complexity: O(n) | ||
์ ํจํ ๋ฌธ์์ด์ด๋ผ๋ฉด, ์ธ์ ํ ์ด๊ณ ๋ซ๋ ๊ดํธ ์์ ํ๋์ฉ ์บ์ฌ์์ผฐ์ ๋, ๋น ๋ฌธ์์ด๋ง ๋จ๊ฒ ๋๋ค. | ||
๋งค์น๋๋ ์์ด ์๋ก ๋จ์ด์ ธ์์ ์ ์๊ธฐ ๋๋ฌธ์, ๊ทธ ์์ ์ ํจํ ์๋ค์ ๋ฏธ๋ฆฌ ๋ชจ๋ ์บ์ฌ์ํจ ๋ค์ ํ๋จํด์ผ ๋งค์นญ ์ฌ๋ถ๋ฅผ ์ฝ๊ฒ ํ๋จํ ์ ์๋๋ฐ, ์ด ๊ณผ์ ์ ์คํ์ ์ด์ฉํด ๊ตฌํํ ์ ์๋ค. | ||
*/ | ||
class Solution { | ||
public boolean isValid(String s) { | ||
Stack<Character> st = new Stack<>(); | ||
|
||
if (s.length() % 2 == 1) { | ||
return false; | ||
} | ||
|
||
for (char ch : s.toCharArray()) { | ||
if (ch == '(' || ch == '{' || ch == '[') { | ||
st.push(ch); | ||
} else { | ||
if (st.empty()) | ||
return false; | ||
if (ch == ')') { | ||
if (st.peek() != '(') | ||
return false; | ||
} else if (ch == '}') { | ||
if (st.peek() != '{') | ||
return false; | ||
} else { | ||
if (st.peek() != '[') | ||
return false; | ||
} | ||
st.pop(); | ||
} | ||
} | ||
return st.empty(); | ||
} | ||
} |