1. Description
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
- https://leetcode.com/problems/xor-operation-in-an-array/