Skip to content

[bky373] Solve week 4 problems #113

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 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions counting-bits/bky373.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x &= (x - 1) 를 통해 맨 오른쪽 1 bit를 지우는게 신기하네요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이번에 로직 개선하면서 배웠습니다 ㅋㅋ 비트 연산은 아무래도 적응 기간이 필요할 것 같네요 😭

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* https://leetcode.com/problems/counting-bits/
*
* time: O(n * log n)
* space: O(1)
*/
class Solution {

public int[] countBits(int n) {
int[] ans = new int[n + 1];
for (int i = 0; i <= n; i++) {
int x = i;
int cnt = 0;
while (x != 0) {
cnt++;
x &= (x - 1);
}
ans[i] = cnt;
}
return ans;
}
}

19 changes: 19 additions & 0 deletions group-anagrams/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* https://leetcode.com/problems/group-anagrams/
*
* time: O(n * m log m)
* space: O(nm)
*/
class Solution {

public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> groups = new HashMap();
for (String str : strs) {
char[] arr = str.toCharArray();
Arrays.sort(arr);
groups.computeIfAbsent(new String(arr), k -> new ArrayList<>())
.add(str);
}
return new ArrayList(groups.values());
}
}
15 changes: 15 additions & 0 deletions missing-number/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* https://leetcode.com/problems/missing-number/
*
* time: O(n)
* space: O(1)
*/
class Solution {
public int missingNumber(int[] nums) {
int sum = nums.length * (nums.length+1) / 2;
for (int num : nums) {
sum -= num;
}
return sum;
}
}
17 changes: 17 additions & 0 deletions number-of-1-bits/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* https://leetcode.com/problems/number-of-1-bits/
*
* time: O(log n)
* space: O(1)
*/
class Solution {

public int hammingWeight(int n) {
int cnt = 0;
while (n != 0) {
cnt++;
n &= (n - 1);
}
return cnt;
}
}
17 changes: 17 additions & 0 deletions reverse-bits/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* https://leetcode.com/problems/reverse-bits/
*
* time: O(1)
* space: O(1)
*/
public class Solution {

public int reverseBits(int n) {
int ans = 0;
for (int i = 0; i < 32; i++) {
ans = ans << 1 | n & 1;
n >>= 1;
}
return ans;
}
}