Skip to content

Commit f13cf0d

Browse files
committed
leetcode 238
python
1 parent 81f9589 commit f13cf0d

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
108108
| 234| Easy | [Palindrome Linked List](leetcode/234-Easy-Palindrome-Linked-List/problem.md) | [Python](leetcode/234-Easy-Palindrome-Linked-List/answer.py) |
109109
| 235| Easy | [Lowest Common Ancestor of a Binary Search Tree](leetcode/235-Easy-Lowest-Common-Ancestor-Of-A-Binary-Search-Tree/problem.md) | [Python](leetcode/235-Easy-Lowest-Common-Ancestor-Of-A-Binary-Search-Tree/answer.py)|
110110
| 236| Medium | [Lowest Common Ancestor of a Binary Tree](leetcode/236-Medium-Lowest-Common-Ancestor-Of-A-Binary-Tree/problem.md) | [Python](leetcode/236-Medium-Lowest-Common-Ancestor-Of-A-Binary-Tree/answer.py)|
111+
| 238| Medium | [Product of Array Except Self](leetcode/238-Medium-Product-Of-Array-Except-Self/problem.md) | [Python](leetcode/238-Medium-Product-Of-Array-Except-Self/answer.py)|
111112
| 258| Easy | [Add Digits](leetcode/235-Easy-Add-Digits/problem.md) | [Python](leetcode/235-Easy-Add-Digits/answer.py) |
112113
| 268| Easy | [Missing Number](leetcode/268-Easy-Missing-Number/problem.md) | [Python](leetcode/268-Easy-Missing-Number/answer.py) |
113114
| 283| Easy | [Move Zeroes](leetcode/283-Easy-Move-Zeroes/problem.md) | [Python](leetcode/283-Easy-Move-Zeroes/answer.py) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
3+
#-------------------------------------------------------------------------------
4+
# Optimal Solution O(n)
5+
#-------------------------------------------------------------------------------
6+
7+
class Solution:
8+
def productExceptSelf(self, nums):
9+
"""
10+
:type nums: List[int]
11+
:rtype: List[int]
12+
"""
13+
p = 1
14+
output = []
15+
16+
# We grow a product from left to right offset by 1
17+
for i in range(len(nums)):
18+
output.append(p)
19+
p *= nums[i]
20+
21+
p = 1
22+
# Now we grow a product from right to left
23+
# The product of the two products will be our desired result
24+
for i in range(len(nums)-1, -1, -1):
25+
output[i] *= p
26+
p *= nums[i]
27+
28+
return output
29+
30+
#-------------------------------------------------------------------------------
31+
# O(n) Cheaty Division Way
32+
#-------------------------------------------------------------------------------
33+
34+
class Solution:
35+
def productExceptSelf(self, nums):
36+
"""
37+
:type nums: List[int]
38+
:rtype: List[int]
39+
"""
40+
output=[0]*len(nums)
41+
zero = nums.count(0)
42+
if zero >= 2:
43+
return output
44+
elif zero == 1:
45+
i = 0
46+
while nums[i] != 0:
47+
i += 1
48+
p = 1
49+
for n in nums[:i] + nums[i+1:]:
50+
p *= n
51+
output[i] = p
52+
return output
53+
else:
54+
p = 1
55+
for n in nums:
56+
p *= n
57+
for i in range(len(nums)):
58+
output[i] = p//nums[i]
59+
return output
60+
61+
#-------------------------------------------------------------------------------
62+
# Simple O(n^2) brute force
63+
#-------------------------------------------------------------------------------
64+
65+
class Solution:
66+
def productExceptSelf(self, nums):
67+
"""
68+
:type nums: List[int]
69+
:rtype: List[int]
70+
"""
71+
output = [1]*len(nums)
72+
for i in range(len(nums)):
73+
for j in range(len(nums)):
74+
if i == j:
75+
continue
76+
output[i] *= nums[j]
77+
return output
78+
#-------------------------------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Problem 238: Product of Array Except Self
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
8+
9+
**Note:** Please solve it without division and in O(n).
10+
11+
#### Example
12+
13+
<pre>
14+
Input: [1,2,3,4]
15+
Output: [24,12,8,6]
16+
</pre>

Diff for: leetcode/461-Easy-Hamming-Distance/answer.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#!/usr/bin/env python3
22

33
#-------------------------------------------------------------------------------
4+
5+
class Solution:
6+
def hammingDistance(self, x, y):
7+
"""
8+
:type x: int
9+
:type y: int
10+
:rtype: int
11+
"""
12+
return bin(x ^ y).count('1')
13+
14+
#-------------------------------------------------------------------------------
15+
416
class Solution(object):
517
def hammingDistance(self, x, y):
618
"""
@@ -22,4 +34,3 @@ def hammingDistance(self, x, y):
2234

2335
return count
2436
#-------------------------------------------------------------------------------
25-
# Testing

0 commit comments

Comments
 (0)