Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit310dc1c

Browse files
authored
Update Product of Array Except Self - Leetcode 238.py
1 parent4ee2d06 commit310dc1c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎Product of Array Except Self - Leetcode 238/Product of Array Except Self - Leetcode 238.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,33 @@ def productExceptSelf(self, nums: List[int]) -> List[int]:
3838

3939
# Time Complexity: O(n)
4040
# Space Complexity: O(n)
41+
42+
43+
# Optimal Solution for Bootcamp
44+
classSolution:
45+
defproductExceptSelf(self,nums:List[int])->List[int]:
46+
n=len(nums)
47+
answer= [0]*n
48+
49+
L=1
50+
left_product= [0]*n
51+
52+
foriinrange(n):
53+
left_product[i]=L
54+
L*=nums[i]
55+
56+
57+
R=1
58+
right_product= [0]*n
59+
60+
foriinrange(n-1,-1,-1):
61+
right_product[i]=R
62+
R*=nums[i]
63+
64+
65+
foriinrange(n):
66+
answer[i]=left_product[i]*right_product[i]
67+
68+
returnanswer
69+
# Time Complexity: O(n)
70+
# Space Complexity: O(n)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp