題目描述
給你兩個整數,n 和 start 。
數組 nums 定義爲:nums[i] = start + 2*i(下標從 0 開始)且 n == nums.length 。
請返回 nums 中所有元素按位異或(XOR)後得到的結果。
例子:
輸入:n = 5, start = 0
輸出:8
解釋:數組 nums 爲 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。
“^” 爲按位異或 XOR 運算符。
解題思路
pyhton 實現
class Solution(object):
def xorOperation(self, n, start):
"""
:type n: int
:type start: int
:rtype: int
"""
res = start
for i in range(1, n):
res ^= start + 2*i
return res