Last active
April 16, 2020 02:53
-
-
Save phatnguyenuit/76f8c68554962ced90f4833c335aaafa to your computer and use it in GitHub Desktop.
30-day-leetcoding-challenges
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Product of Array Except Self | |
def productExceptSelf(nums): | |
size = len(nums) | |
result = [1] * size | |
left = 1 | |
right = 1 | |
for i in range(size): | |
result[i] *= left | |
result[-1-i] *= right | |
left *= nums[i] | |
right *= nums[-1-i] | |
return result | |
if __name__ == '__main__': | |
print(productExceptSelf([1, 2, 3, 4])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a non-empty array of integers, every element appears twice except for one. | |
# Find that single one. | |
# Example: input [1, 2, 3, 2, 3] => output 1 | |
# Explain: | |
# a ^ 0 => a | |
# a ^ a => 0 | |
# [a, b, a] => a ^ b ^ a = (a ^ a) ^ b => b | |
def single_number(nums): | |
return reduce(lambda x, y: x^y, nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment