chencl1986 / Blog

Welcome to lee's blog.
464 stars 85 forks source link

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

Open chencl1986 opened 1 year ago

chencl1986 commented 1 year ago

原题链接: 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
}