본문 바로가기
  • Jetpack 알아보기
LeetCode/Easy

[Java] LeetCode 1486 : XOR Operation in an Array

by 새우버거♬ 2021. 4. 17.

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

 

ggujangi/ggu.leet-code

LeetCode, Java. Contribute to ggujangi/ggu.leet-code development by creating an account on GitHub.

github.com

 

 

 

 

출처 : leetCode