Leetcode 1486. XOR Operation in an Array

2021-08-13 11:54:02 浏览数 (1)

1. Description

2. Solution

**解析:**Version 1,初始值设为0,因为任何数异或0都不改变其值,按规则依次计算数组中的值,然后进行异或运算即可。

  • Version 1
代码语言:javascript复制
class Solution:
    def xorOperation(self, n: int, start: int) -> int:
        result = 0
        for i in range(n):
            x = start   2 * i
            result ^= x
        return result

Reference

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

0 人点赞