자기 자신을 제외한 모든 원소들의 곱들을 산출하시오.
https://leetcode.com/problems/product-of-array-except-self/
import numpy as np
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
answer = np.array([1]*len(nums))
for i,n in enumerate(nums):
multiplication = answer*n - answer
multiplication[i] = 0
answer += multiplication
return answer
array로 각 원소의 곱만큼 더하기 방식으로 진행했다.
단, 해당 원소는 0을 더함으로 조건을 만족하였다.
하지만, Time Out. ㅎㅎ
array 자체로 계산하는 것이 O(n)...
Next to do.
1. 왼쪽 곱셈에 결과에 오른쪽 값을 차례대로 곱셈하는 방식으로 풀자