Skip to content

Commit 5408ba1

Browse files
committed
product of array except self
1 parent ad4c0a7 commit 5408ba1

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
풀이 :
3+
for문을 통해 맨 앞, 맨 뒤부터 순회하면서 곱연산을 함.
4+
5+
복잡도 계산 :
6+
매개변수 nums의 길이 -> N
7+
시간 복잡도 : O(N)
8+
공간 복잡도 : O(N)
9+
*/
10+
11+
class Solution {
12+
public int[] productExceptSelf(int[] nums) {
13+
int[] resultList=new int[nums.length];
14+
Arrays.fill(resultList,1);
15+
int result = 1;
16+
for(int i=0; i<nums.length-1;i++){
17+
result*=nums[i];
18+
resultList[i+1]*=result;
19+
}
20+
result = 1;
21+
for(int i=nums.length-1; i>0;i--){
22+
result*=nums[i];
23+
resultList[i-1]*=result;
24+
}
25+
return resultList;
26+
}
27+
}

0 commit comments

Comments
 (0)