Skip to content

Instantly share code, notes, and snippets.

@terracotta-ko
Created September 22, 2021 02:51
leetcode 238
class Solution {
fun productExceptSelf(nums: IntArray): IntArray {
val ans = IntArray(nums.size) {1}
//>> product of left
for(i in 1..nums.lastIndex) {
ans[i] = nums[i-1] * ans[i-1]
}
//>> product of right
var rightP = 1
for(i in nums.lastIndex downTo 0) {
ans[i] *= rightP
rightP *= nums[i]
}
return ans
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment