美文网首页Leetcode
Leetcode 1486. XOR Operation in

Leetcode 1486. XOR Operation in

作者: SnailTyan | 来源:发表于2021-09-09 15:49 被阅读0次

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

XOR Operation in an Array

2. Solution

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

  • Version 1
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/

相关文章

网友评论

    本文标题:Leetcode 1486. XOR Operation in

    本文链接:https://www.haomeiwen.com/subject/ncecbltx.html