File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Constraints:
3+ 1. 2 <= nums.length <= 10^5
4+ 2. -30 <= nums[i] <= 30
5+ 3. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer
6+
7+ Time Complexity: O(n)
8+ - λ°°μ΄μ λ λ² μννλ―λ‘ O(n)
9+
10+ Space Complexity: O(1)
11+ - μΆλ ₯ λ°°μ΄(answer)μ μ μΈνλ©΄ μΆκ° 곡κ°μ΄ μμλ§νΌλ§ νμ(left, right λ³μ)
12+
13+ νμ΄ λ°©λ²:
14+ 1. answer λ°°μ΄μ 1λ‘ μ΄κΈ°ν (κ³±μ
μμλ 1μ΄ μν₯μ μ£Όμ§ μμ)
15+ 2. μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ μν:
16+ - answer[i]μ νμ¬κΉμ§μ left λμ κ°μ κ³±ν¨
17+ - left *= nums[i]λ‘ λ€μμ μν΄ left κ°μ μ
λ°μ΄νΈ
18+ 3. μ€λ₯Έμͺ½μμ μΌμͺ½μΌλ‘ μν (range(n-1, -1, -1) μ¬μ©):
19+ - answer[i]μ νμ¬κΉμ§μ right λμ κ°μ κ³±ν¨
20+ - right *= nums[i]λ‘ λ€μμ μν΄ right κ°μ μ
λ°μ΄νΈ
21+ """
22+
23+ class Solution :
24+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
25+ n = len (nums )
26+ answer = [1 ] * n
27+
28+ left = 1
29+ for i in range (n ):
30+ answer [i ] *= left
31+ left *= nums [i ]
32+
33+ right = 1
34+ for i in range (n - 1 , - 1 , - 1 ):
35+ answer [i ] *= right
36+ right *= nums [i ]
37+
38+ return answer
You canβt perform that action at this time.
0 commit comments