Skip to content

数组异或操作

仲灏2022-11-08约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/f25ca3.md for this page in Markdown format</div>

数组异或操作

CategoryDifficultyLikesDislikes
algorithmsEasy (85.68%)117-

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>

给你两个整数,nstart

数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length

请返回 nums 中所有元素按位异或(XOR)后得到的结果。

示例 1:

输入:n = 5, start = 0
输出:8
解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。
     "^" 为按位异或 XOR 运算符。

示例 2:

输入:n = 4, start = 3
输出:8
解释:数组 nums 为 [3, 5, 7, 9],其中 (3 ^ 5 ^ 7 ^ 9) = 8.

示例 3:

输入:n = 1, start = 7
输出:7

示例 4:

输入:n = 10, start = 5
输出:2

提示:

  • 1 <= n <= 1000
  • 0 <= start <= 1000
  • n == nums.length

Discussion | Solution

tsx
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-08 11:02:34
 * @LastEditTime: 2022-11-08 13:18:58
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:
 * @FilePath: /loan-home/Users/izhaong/izhaong/Project_me/leetcode/1486.数组异或操作.ts
 */
/*
 * @lc app=leetcode.cn id=1486 lang=typescript
 *
 * [1486] 数组异或操作
 *
 * https://leetcode.cn/problems/xor-operation-in-an-array/description/
 *
 * algorithms
 * Easy (85.68%)
 * Likes:    117
 * Dislikes: 0
 * Total Accepted:    79.2K
 * Total Submissions: 92.4K
 * Testcase Example:  '5\n0'
 *
 * 给你两个整数,n 和 start 。
 *
 * 数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。
 *
 * 请返回 nums 中所有元素按位异或(XOR)后得到的结果。
 *
 *
 *
 * 示例 1:
 *
 * 输入:n = 5, start = 0
 * 输出:8
 * 解释:数组 nums 为 [0, 2, 4, 6, 8],其中 (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8 。
 * ⁠    "^" 为按位异或 XOR 运算符。
 *
 *
 * 示例 2:
 *
 * 输入:n = 4, start = 3
 * 输出:8
 * 解释:数组 nums 为 [3, 5, 7, 9],其中 (3 ^ 5 ^ 7 ^ 9) = 8.
 *
 * 示例 3:
 *
 * 输入:n = 1, start = 7
 * 输出:7
 *
 *
 * 示例 4:
 *
 * 输入:n = 10, start = 5
 * 输出:2
 *
 *
 *
 *
 * 提示:
 *
 *
 * 1 <= n <= 1000
 * 0 <= start <= 1000
 * n == nums.length
 *
 *
 */

// @lc code=start
function xorOperation(n: number, start: number): number {
  let ans = 0;
  for (let i = 0; i < n; i++) {
    ans ^= start + 2 * i;
  }
  return ans;
}
// @lc code=end