-
Notifications
You must be signed in to change notification settings - Fork 0
/
238-Product-of-Array-Except-Self.py
59 lines (45 loc) · 1.57 KB
/
238-Product-of-Array-Except-Self.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
# -----------------------------------------------
# v1 solution (70.66%) --------------------------
# -----------------------------------------------
class Solution:
# @param {integer[]} nums
# @return {integer[]}
def productExceptSelf(self, nums):
# count len of nums[]
n = len(nums)
output = []
left2right=[]
right2left=[]
tmpnumL = 1
tmpnumR = 1
# multiply left to right and right to left
for i in range(0,n):
left2right.append(tmpnumL)
tmpnumL = tmpnumL * nums[i]
right2left.append(tmpnumR)
tmpnumR = tmpnumR * nums[(n-1)-i]
# multiply each
for i in range(0,n):
output.append( right2left[(n-1)-i] * left2right[i] )
return output
# -----------------------------------------------
# v2 solution (84.95%) --------------------------
# -----------------------------------------------
class Solution2:
# @param {integer[]} nums
# @return {integer[]}
def productExceptSelf(self, nums):
# count len of nums[]
n = len(nums)
output = [1] * n
tmpnumL = 1
tmpnumR = 1
# multiply left to right
for i in range(0,n-1):
tmpnumL = tmpnumL * nums[i]
output[i+1] = tmpnumL
# multiply right to left
for i in range((n-1),0,-1):
tmpnumR = tmpnumR * nums[i]
output[i-1] *= tmpnumR
return output