Skip to content

Commit a7f0f96

Browse files
authored
Merge pull request #784 from river20s/main
2 parents 4fe8ea0 + f4a5683 commit a7f0f96

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

reverse-bits/river20s.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution {
2+
/* T.C = O(1)
3+
* S.C = O(1)
4+
*/
5+
public int reverseBits(int n) {
6+
// Set the output to 0
7+
int output = 0;
8+
// Repeat 32 times
9+
for (int i = 0; i < 32; i++) {
10+
// Shift the output value one space to the left to make room for the new bit
11+
output <<= 1;
12+
// '&' operation to get the rightmost bit and add it to the output
13+
output = output | (n & 1);
14+
// Discard the rightmost bit of the 'n'
15+
n = n >> 1;
16+
}
17+
18+
return output;
19+
20+
}
21+
}
22+

two-sum/river20s.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* T.C: O(n) -> 배열 nums를 한 번 순회
3+
* S.C: O(n) → 최대 n개의 요소가 저장됨
4+
*/
5+
import java.util.HashMap;
6+
7+
class Solution {
8+
public int[] twoSum(int[] nums, int target) {
9+
10+
HashMap<Integer, Integer> map = new HashMap<>();
11+
for (int i = 0; i < nums.length; i++) {
12+
int k = target - nums[i];
13+
14+
if (map.containsKey(k)) {
15+
return new int[] { map.get(k), i };
16+
}
17+
18+
map.put(nums[i], i);
19+
}
20+
throw new IllegalArgumentException("exception handling for java compilation");
21+
22+
}
23+
}
24+

0 commit comments

Comments
 (0)