Skip to content

Instantly share code, notes, and snippets.

@willwangcc
Created April 1, 2018 08:11
Show Gist options
  • Save willwangcc/1f14373ae9dfd91d25a1f65959944026 to your computer and use it in GitHub Desktop.
Save willwangcc/1f14373ae9dfd91d25a1f65959944026 to your computer and use it in GitHub Desktop.
elegant code: reduce()?
# Time: O(n)
# Space: O(n)
# Before vs After
#####################################
## After
#####################################
class Solution(object):
def xorGame(self, nums):
return reduce(operator.xor, nums) == 0 or len(nums) % 2 == 0
#####################################
## Before
#####################################
class Solution(object):
def xorGame(self, nums):
n = 0
for num in nums:
n = n^num
if n==0:
return True
return len(nums)%2==0
"""
:type nums: List[int]
:rtype: bool
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment