Skip to content
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

[river20s] WEEK 03 #784

Merged
merged 2 commits into from
Dec 28, 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
22 changes: 22 additions & 0 deletions reverse-bits/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {
/* T.C = O(1)
* S.C = O(1)
*/
public int reverseBits(int n) {
// Set the output to 0
int output = 0;
// Repeat 32 times
for (int i = 0; i < 32; i++) {
// Shift the output value one space to the left to make room for the new bit
output <<= 1;
// '&' operation to get the rightmost bit and add it to the output
output = output | (n & 1);
// Discard the rightmost bit of the 'n'
n = n >> 1;
}

return output;

}
}

24 changes: 24 additions & 0 deletions two-sum/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* T.C: O(n) -> 배열 nums를 한 번 순회
* S.C: O(n) → 최대 n개의 요소가 저장됨
*/
import java.util.HashMap;

class Solution {
public int[] twoSum(int[] nums, int target) {

HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int k = target - nums[i];

if (map.containsKey(k)) {
return new int[] { map.get(k), i };
}

map.put(nums[i], i);
}
throw new IllegalArgumentException("exception handling for java compilation");

}
}

Loading