-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
78 lines (69 loc) · 2.26 KB
/
answer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
#-------------------------------------------------------------------------------
# Optimal Solution O(n)
#-------------------------------------------------------------------------------
class Solution:
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
p = 1
output = []
# We grow a product from left to right offset by 1
for i in range(len(nums)):
output.append(p)
p *= nums[i]
p = 1
# Now we grow a product from right to left
# The product of the two products will be our desired result
for i in range(len(nums)-1, -1, -1):
output[i] *= p
p *= nums[i]
return output
#-------------------------------------------------------------------------------
# O(n) Cheaty Division Way
#-------------------------------------------------------------------------------
class Solution:
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
output=[0]*len(nums)
zero = nums.count(0)
if zero >= 2:
return output
elif zero == 1:
i = 0
while nums[i] != 0:
i += 1
p = 1
for n in nums[:i] + nums[i+1:]:
p *= n
output[i] = p
return output
else:
p = 1
for n in nums:
p *= n
for i in range(len(nums)):
output[i] = p//nums[i]
return output
#-------------------------------------------------------------------------------
# Simple O(n^2) brute force
#-------------------------------------------------------------------------------
class Solution:
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
output = [1]*len(nums)
for i in range(len(nums)):
for j in range(len(nums)):
if i == j:
continue
output[i] *= nums[j]
return output
#-------------------------------------------------------------------------------