LeetCode # 1486
Given an integer n and an integer start.
Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length.
Return the bitwise XOR of all elements of nums.
Example 1:
Input: n = 5, start = 0
Output: 8
Explanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8. Where "^" corresponds to bitwise XOR operator.
Note:
- 1 <= n <= 1000
- 0 <= start <= 1000
- n == nums.length
Solution #1 (0ms)
class Solution {
public int xorOperation(int n, int start) {
int answer = 0;
for (int i = 0; i < n; i++) {
answer ^= (start + i * 2);
}
return answer;
}
}
Result #1
More Algorithm!
👇👇
github.com/ggujangi/ggu.leet-code
출처 : leetCode
'LeetCode > Easy' 카테고리의 다른 글
[Java] LeetCode 290 : Word Pattern (0) | 2021.04.17 |
---|---|
[Java] LeetCode 914 : X of a Kind in a Deck of Cards (0) | 2021.04.17 |
[Java] LeetCode 977 : Squares of a Sorted Array (0) | 2021.03.30 |
[Java] LeetCode 1295 : Find Numbers with Even Number of Digits (0) | 2021.03.30 |
[Java] LeetCode 485 : Max Consecutive Ones (0) | 2021.03.30 |