-
[leetcode 75-4] Product of Array Except SelfAlgorithm 2023. 1. 29. 22:24
https://leetcode.com/problems/product-of-array-except-self/
Product of Array Except Self - LeetCode
Product of Array Except Self - Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
leetcode.com
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: ans, suf, pre = [1]*len(nums), 1, 1 for i in range(len(nums)): ans[i] *= pre pre *= nums[i] ans[-1-i] *= suf suf *= nums[-1-i] return ans
'Algorithm' 카테고리의 다른 글
[leetcode 75-6] Maximum Product Subarray (0) 2023.01.29 [leetcode 75-5] Maximum Subarray (1) 2023.01.29 [leetcode 75-3] Containes Duplicate (1) 2023.01.29 [leetcode 75-2] Best Time to Buy and Sell Stock II (0) 2023.01.29 [leetcode 75-1] Two Sum (1) 2023.01.28