-
-
Notifications
You must be signed in to change notification settings - Fork 194
[박종훈] 4주차 답안 제출 #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[박종훈] 4주차 답안 제출 #94
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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; | ||
} | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.