Skip to content

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

@chencl1986

Description

@chencl1986

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions