目次
トピック
1486.配列XOR演算
は、nとstartの2つの整数を提供します。
配列numsは、nums [i] = start + 2 * i(添え字は0から始まります)およびn == nums.lengthとして定義されます。
すべての要素のビット単位の排他的論理和(XOR)の結果をnumsで返します。
例1:
输入:n = 5, start = 0
输出:8
解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。
"^" 为按位异或 XOR 运算符。
例2:
输入:n = 4, start = 3
输出:8
解释:数组 nums 为 [3, 5, 7, 9],其中 (3 ^ 5 ^ 7 ^ 9) = 8.
例3:
输入:n = 1, start = 7
输出:7
例4:
输入:n = 10, start = 5
输出:2
促す:
1 <= n <= 1000
0 <= start <= 1000
n == nums.length
問題解決(Java)
class Solution
{
public int xorOperation(int n, int start)
{
//初始化nums数组
int[] nums = new int[n];
for(int index = 0;index < n;index++)
{
nums[index] = start + 2 * index;
}
int result = nums[0];
for(int index = 1;index < n;index++)
{
result = result^nums[index];
}
return result;
}
}