Skip to content

Commit d6581b4

Browse files
committed
Time: 292 ms (89.97%), Space: 45.4 MB (53.29%) - LeetHub
1 parent 992e5a2 commit d6581b4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
fun productExceptSelf(nums: IntArray): IntArray {
3+
4+
val length = nums.size
5+
6+
val left = IntArray(length)
7+
val right = IntArray(length)
8+
9+
val answer = IntArray(length)
10+
11+
left[0] = 1
12+
right[nums.size - 1] = 1
13+
14+
for(i in 1 until length){
15+
left[i] = nums[i - 1] * left[i - 1]
16+
}
17+
18+
for(i in length-2 downTo 0 ){
19+
right[i] = nums[i + 1] * right[i + 1]
20+
}
21+
22+
for(i in 0 until nums.size){
23+
answer[i] = left[i] * right[i]
24+
}
25+
26+
return answer
27+
28+
}
29+
}

0 commit comments

Comments
 (0)