Leetcode

Leetcode 1486. XOR Operation in

2021-09-09  本文已影响0人  SnailTyan

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

1. Description

XOR Operation in an Array

2. Solution

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

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/
上一篇下一篇

猜你喜欢

热点阅读