-
Notifications
You must be signed in to change notification settings - Fork 0
/
238-1.py
46 lines (35 loc) · 1.01 KB
/
238-1.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
l_a = []
total_l = 1
for num in nums:
total_l *= num
l_a.append(total_l)
total_r = 1
r_a = [0 for i in xrange(0, len(nums))]
for i in xrange(len(nums)-1, -1, -1):
total_r *= nums[i]
r_a[i] = total_r
array = []
for i in xrange(0, len(nums)):
if i-1 >= 0:
l = l_a[i-1]
else:
l = 1
if i+1 >= len(nums):
r = 1
else:
r = r_a[i+1]
array.append(l * r)
return array
if __name__ == "__main__":
print Solution().productExceptSelf([0, 0])
print Solution().productExceptSelf([1,2,3,4,0])
print Solution().productExceptSelf([1, 2, 3, 4])