Skip to content

Instantly share code, notes, and snippets.

@phatnguyenuit
Last active April 16, 2020 02:53
Show Gist options
  • Save phatnguyenuit/76f8c68554962ced90f4833c335aaafa to your computer and use it in GitHub Desktop.
Save phatnguyenuit/76f8c68554962ced90f4833c335aaafa to your computer and use it in GitHub Desktop.
30-day-leetcoding-challenges
# 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]))
# 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