chencl1986 / Blog

Welcome to lee's blog.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LeetCode题解:1486. 数组异或操作,模拟,JavaScript,详细注释

chencl1986 opened this issue · comments

原题链接:
https://leetcode.cn/problems/xor-operation-in-an-array/

解题思路:

  1. 根据题意,初始值为start,按照nums[i] = start + 2 * i依次进行异或运算即可。
/**
 * @param {number} n
 * @param {number} start
 * @return {number}
 */
var xorOperation = function (n, start) {
  let result = start // 初始值为start

  for (let i = 1; i < n; i++) {
    // 根据题意,逐个进行异或运算
    result ^= start + 2 * i
  }

  return result
}