소프트웨어에 대한 모든 것

LeetCode 풀기 - 1486. XOR Operation in an Array 본문

알고리즘/LeetCode

LeetCode 풀기 - 1486. XOR Operation in an Array

앤테바 2021. 12. 2. 06:22
반응형

1486. XOR Operation in an Array

https://leetcode.com/problems/xor-operation-in-an-array/

 

XOR Operation in an Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

class Solution:
    def xorOperation(self, n: int, start: int) -> int:        
        res = start
        for i in range(1, n):
            res ^= start + 2*i
        return res

솔루션2)

class Solution:
    def xorOperation(self, n: int, start: int) -> int:        
        return reduce(lambda x,y: x^y, range(start, start+n*2,2))

 

반응형
Comments