Skip to content

Commit 4306525

Browse files
committed
product-of-array-except-self
1 parent 356bfbb commit 4306525

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def productExceptSelf(self, nums: List[int]) -> List[int]:
3+
# ans = [1] * len(nums)
4+
# for i in range(len(nums)):
5+
# for j in range(len(nums)):
6+
# if i != j:
7+
# ans[i] = ans[i] * nums[j]
8+
# return ans
9+
# 시간복잡도를 줄이기 위해서 이중루프를 안쓰는 방법으로
10+
left = [1] * len(nums)
11+
right = [1] * len(nums)
12+
for i in range(len(nums) - 1):
13+
left[i+1] = left[i] * nums[i]
14+
print(left)
15+
for j in range(len(nums) - 1, 0, -1):
16+
right[j-1] = right[j] * nums[j]
17+
print(right)
18+
19+
answer = []
20+
for k in range(len(left)):
21+
answer.append(left[k]*right[k])
22+
return answer

0 commit comments

Comments
 (0)