File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution (object ):
2+ def productExceptSelf (self , nums ):
3+ """
4+ :type nums: List[int]
5+ :rtype: List[int]
6+ """
7+
8+ all_multi = 1
9+ zero_count = 0
10+ result = []
11+
12+ for v in nums :
13+ if v == 0 :
14+ zero_count += 1
15+ continue
16+ all_multi *= v
17+ all_multi = int (all_multi )
18+
19+ if zero_count == 0 :
20+ for v in nums :
21+ if v == 0 :
22+ result .append (all_multi )
23+ else :
24+ cur = all_multi / v
25+ result .append (int (cur ))
26+ elif zero_count == 1 :
27+ for v in nums :
28+ if v == 0 :
29+ result .append (all_multi )
30+ else :
31+ result .append (0 )
32+
33+ if zero_count >= 2 :
34+ return [0 for _ in range (len (nums ))]
35+
36+ return result
37+
38+
39+ if __name__ == "__main__" :
40+ solution = Solution ()
41+ # nums = [1,2,3,4]
42+ # nums = [-1,1,0,-3,3]
43+ nums = [1 ,2 ,3 ,4 ,0 ,0 ]
44+ result = solution .productExceptSelf (nums )
45+ print (result )
46+
47+
48+
49+
You can’t perform that action at this time.
0 commit comments