LeetCode #1486. XOR Operation in an Array
题目
1486. XOR Operation in an Array
解题方法
设置rat = num = start,从1开始做循环到n-1,先计算num = start + 2*i,然后用计算rat^=num,最后返回rat。
时间复杂度:O(n)
空间复杂度:O(1)
代码
class Solution:
def xorOperation(self, n: int, start: int) -> int:
rat = num = start
for i in range(1, n):
num = start + 2 * i
rat ^= num
return rat