Skip to content

Commit 024836a

Browse files
committed
product of array except self solved
1 parent 5747260 commit 024836a

File tree

1 file changed

+23
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)