Created
April 1, 2018 08:11
-
-
Save willwangcc/1f14373ae9dfd91d25a1f65959944026 to your computer and use it in GitHub Desktop.
elegant code: reduce()?
This file contains hidden or 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
# 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