<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/05769c.md for this page in Markdown format</div>
解码异或后的数组
| Category | Difficulty | Likes | Dislikes |
|---|---|---|---|
| algorithms | Easy (86.42%) | 106 | - |
<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>
未知 整数数组 arr 由 n 个非负整数组成。
经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。
给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。
请解码返回原数组 arr 。可以证明答案存在并且是唯一的。
示例 1:
输入:encoded = [1,2,3], first = 1
输出:[1,0,2,1]
解释:若 arr = [1,0,2,1] ,那么 first = 1 且 encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]示例 2:
输入:encoded = [6,2,7,3], first = 4
输出:[4,2,0,7,4]提示:
2 <= n <= 104encoded.length == n - 10 <= encoded[i] <= 1050 <= first <= 105
/*
* @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Date: 2022-11-07 10:08:15
* @LastEditTime: 2022-11-07 10:29:12
* @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
* @Description:
* @FilePath: /leetcode/1720.解码异或后的数组.ts
*/
/*
* @lc app=leetcode.cn id=1720 lang=typescript
*
* [1720] 解码异或后的数组
*
* https://leetcode.cn/problems/decode-xored-array/description/
*
* algorithms
* Easy (86.42%)
* Likes: 106
* Dislikes: 0
* Total Accepted: 53.1K
* Total Submissions: 61.4K
* Testcase Example: '[1,2,3]\n1'
*
* 未知 整数数组 arr 由 n 个非负整数组成。
*
* 经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1]
* 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。
*
* 给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。
*
* 请解码返回原数组 arr 。可以证明答案存在并且是唯一的。
*
*
*
* 示例 1:
*
*
* 输入:encoded = [1,2,3], first = 1
* 输出:[1,0,2,1]
* 解释:若 arr = [1,0,2,1] ,那么 first = 1 且 encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] =
* [1,2,3]
*
*
* 示例 2:
*
*
* 输入:encoded = [6,2,7,3], first = 4
* 输出:[4,2,0,7,4]
*
*
*
*
* 提示:
*
*
* 2
* encoded.length == n - 1
* 0
* 0
*
*
*/
// @lc code=start
function decode(encoded: number[], first: number): number[] {
const decodeLen = encoded.length + 1;
const decodeArr = new Array(decodeLen);
decodeArr[0] = first;
for (let i = 1; i < decodeLen; i++) {
decodeArr[i] = decodeArr[i - 1] ^ encoded[i - 1];
}
return decodeArr;
}
// @lc code=end