/product_of_array_except_self.kt Secret
Created
September 22, 2021 02:51
leetcode 238
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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