1720:解码异或后的数组

This commit is contained in:
huangge1199@hotmail.com 2021-05-06 21:25:49 +08:00
parent 1f180f5c57
commit fd22567cc2
4 changed files with 106 additions and 2 deletions

View File

@ -0,0 +1,68 @@
//未知 整数数组 arr n 个非负整数组成
//
// 经编码后变为长度为 n - 1 的另一个整数数组 encoded 其中 encoded[i] = arr[i] XOR arr[i + 1] 例如a
//rr = [1,0,2,1] 经编码后得到 encoded = [1,2,3]
//
// 给你编码后的数组 encoded 和原数组 arr 的第一个元素 firstarr[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 <= 104
// encoded.length == n - 1
// 0 <= encoded[i] <= 105
// 0 <= first <= 105
//
// Related Topics 位运算
// 👍 56 👎 0
package leetcode.editor.cn;
//1720:解码异或后的数组
public class DecodeXoredArray {
public static void main(String[] args) {
//测试代码
Solution solution = new DecodeXoredArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] decode(int[] encoded, int first) {
int size = encoded.length;
int[] result = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
if (i == 0) {
result[i] = first;
} else {
result[i] = result[i - 1] ^ encoded[i - 1];
}
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,36 @@
<p><strong>未知</strong> 整数数组 <code>arr</code><code>n</code> 个非负整数组成。</p>
<p>经编码后变为长度为 <code>n - 1</code> 的另一个整数数组 <code>encoded</code> ,其中 <code>encoded[i] = arr[i] XOR arr[i + 1]</code> 。例如,<code>arr = [1,0,2,1]</code> 经编码后得到 <code>encoded = [1,2,3]</code></p>
<p>给你编码后的数组 <code>encoded</code> 和原数组 <code>arr</code> 的第一个元素 <code>first</code><code>arr[0]</code>)。</p>
<p>请解码返回原数组 <code>arr</code> 。可以证明答案存在并且是唯一的。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>encoded = [1,2,3], first = 1
<strong>输出:</strong>[1,0,2,1]
<strong>解释:</strong>若 arr = [1,0,2,1] ,那么 first = 1 且 encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>encoded = [6,2,7,3], first = 4
<strong>输出:</strong>[4,2,0,7,4]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 <= n <= 10<sup>4</sup></code></li>
<li><code>encoded.length == n - 1</code></li>
<li><code>0 <= encoded[i] <= 10<sup>5</sup></code></li>
<li><code>0 <= first <= 10<sup>5</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 56</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long