-
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 #94 from dev-jonghoonpark/main
[박종훈] 4주차 답안 제출
- Loading branch information
Showing
5 changed files
with
121 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,20 @@ | ||
- https://leetcode.com/problems/counting-bits/ | ||
- time complexity : O(n \* log n), logn 이 붙는 이유는 bit는 log를 따라 수가 결정되기 때문 | ||
- space complexity : O(n) | ||
- https://algorithm.jonghoonpark.com/2024/04/23/leetcode-338 | ||
|
||
```java | ||
public int[] countBits(int n) { | ||
int result[] = new int[n + 1]; | ||
for (int i = 0; i <= n; i++) { | ||
int num = i; | ||
int count = 0; | ||
while (num > 0) { | ||
count += num & 1; | ||
num = num >> 1; | ||
} | ||
result[i] = count; | ||
} | ||
return result; | ||
} | ||
``` |
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,33 @@ | ||
- https://leetcode.com/problems/group-anagrams/ | ||
- time complexity : O(n \* m log m), 여기서 m은 str 배열(strs)의 각 str의 평균이다. | ||
- space complexity : O(n \* m) | ||
- https://algorithm.jonghoonpark.com/2024/05/25/leetcode-49 | ||
|
||
```java | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
List<List<String>> result = new ArrayList<>(); | ||
HashMap<String, Integer> map = new HashMap<>(); | ||
|
||
for(String str: strs) { | ||
char[] temp = str.toCharArray(); | ||
Arrays.sort(temp); | ||
String sorted = String.valueOf(temp); | ||
if(map.containsKey(sorted)) { | ||
result.get(map.get(sorted)).add(str); | ||
} else { | ||
int newIndex = result.size(); | ||
List<String> newArrayList = new ArrayList<>(); | ||
result.add(newArrayList); | ||
newArrayList.add(str); | ||
map.put(sorted, newIndex); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
``` | ||
|
||
## TC, SC | ||
|
||
시간 복잡도는 O(n \* m log m)이고, 공간 복잡도는 O(n \* m)이다. | ||
여기서 m은 str 배열(strs)의 각 str의 평균이다. |
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 @@ | ||
- https://leetcode.com/problems/missing-number/ | ||
- time complexity : O(n) | ||
- space complexity : O (n) | ||
- https://algorithm.jonghoonpark.com/2024/05/25/leetcode-268 | ||
|
||
```java | ||
public int missingNumber(int[] nums) { | ||
int[] counts = new int[nums.length + 1]; | ||
|
||
for(int num : nums) { | ||
counts[num] = 1; | ||
} | ||
|
||
for(int i = 0; i < counts.length; i ++){ | ||
if(counts[i] == 0) { | ||
return i; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
``` | ||
|
||
등차수열로 푸는 방법도 있는 재밌는 문제. | ||
|
||
```java | ||
public int missingNumber(int[] arr) { | ||
int sum = 0; | ||
int max = (arr.length * (arr.length + 1)) / 2; | ||
for (int i = 0; i < arr.length; i++) { | ||
sum += arr[i]; | ||
} | ||
return max - sum; | ||
} | ||
``` |
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,15 @@ | ||
- https://leetcode.com/problems/number-of-1-bits/ | ||
- time complexity : O(logn) | ||
- space complexity : O(1) | ||
- https://algorithm.jonghoonpark.com/2024/02/20/leetcode-191 | ||
|
||
```java | ||
public int hammingWeight(int n) { | ||
int count = 0; | ||
while (n != 0) { | ||
count += n % 2; | ||
n = n >> 1; | ||
} | ||
return count; | ||
} | ||
``` |
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,18 @@ | ||
- https://leetcode.com/problems/reverse-bits/ | ||
- time complexity : O(1) | ||
- space complexity : O(1) | ||
- https://algorithm.jonghoonpark.com/2024/04/23/leetcode-190 | ||
|
||
```java | ||
public class Solution { | ||
public int reverseBits(int n) { | ||
int ans = 0; | ||
for (int i = 0; i < 32; i++) { | ||
ans <<= 1; | ||
ans |= (n & 1); | ||
n >>= 1; | ||
} | ||
return ans; | ||
} | ||
} | ||
``` |