Skip to content

Commit 1d20733

Browse files
committed
🎨 reverse bits, product of array except self Solution
1 parent 2d97fea commit 1d20733

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* 시간복잡도: O(n)
3+
* 공간복잡도: O(1)
4+
* */
5+
class Solution {
6+
public int[] productExceptSelf(int[] nums) {
7+
int n = nums.length;
8+
int[] answer = new int[n];
9+
10+
answer[0] = 1;
11+
for (int i = 1; i < n; i++) {
12+
answer[i] = answer[i - 1] * nums[i - 1];
13+
}
14+
15+
int suffixProduct = 1;
16+
for (int i = n - 1; i >= 0; i--) {
17+
answer[i] *= suffixProduct;
18+
suffixProduct *= nums[i];
19+
}
20+
21+
return answer;
22+
}
23+
}

reverse-bits/dalpang81.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* 시간복잡도: O(1)
3+
* 공간복잡도: O(1)
4+
* */
5+
public class Solution {
6+
// you need treat n as an unsigned value
7+
public int reverseBits(int n) {
8+
int result = 0;
9+
for (int i = 0; i < 32; i++) {
10+
result <<= 1;
11+
result |= (n & 1);
12+
n >>= 1;
13+
}
14+
return result;
15+
}
16+
}

0 commit comments

Comments
 (0)