1310:子数组异或查询

This commit is contained in:
huangge1199@hotmail.com 2021-05-12 22:56:42 +08:00
parent 46d90a5448
commit 0995757c72
3 changed files with 116 additions and 1 deletions

View File

@ -0,0 +1,72 @@
//有一个正整数数组 arr现给你一个对应的查询数组 queries其中 queries[i] = [Li, Ri]
//
// 对于每个查询 i请你计算从 Li Ri XOR arr[Li] xor arr[Li+1] xor ... xor arr[Ri]作为
//本次查询的结果
//
// 并返回一个包含给定查询 queries 所有结果的数组
//
//
//
// 示例 1
//
// 输入arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
//输出[2,7,14,8]
//解释
//数组中元素的二进制表示形式是
//1 = 0001
//3 = 0011
//4 = 0100
//8 = 1000
//查询的 XOR 值为
//[0,1] = 1 xor 3 = 2
//[1,2] = 3 xor 4 = 7
//[0,3] = 1 xor 3 xor 4 xor 8 = 14
//[3,3] = 8
//
//
// 示例 2
//
// 输入arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
//输出[8,0,4,4]
//
//
//
//
// 提示
//
//
// 1 <= arr.length <= 3 * 10^4
// 1 <= arr[i] <= 10^9
// 1 <= queries.length <= 3 * 10^4
// queries[i].length == 2
// 0 <= queries[i][0] <= queries[i][1] < arr.length
//
// Related Topics 位运算
// 👍 112 👎 0
package leetcode.editor.cn;
//1310:子数组异或查询
public class XorQueriesOfASubarray {
public static void main(String[] args) {
//测试代码
Solution solution = new XorQueriesOfASubarray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] xorQueries(int[] arr, int[][] queries) {
for (int i = 1; i < arr.length; i++) {
arr[i] = arr[i - 1] ^ arr[i];
}
int[] result = new int[queries.length];
for (int i = 0; i < queries.length; i++) {
result[i] = arr[queries[i][1]] ^ (queries[i][0] == 0 ? 0 : arr[queries[i][0] - 1]);
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,43 @@
<p>有一个正整数数组&nbsp;<code>arr</code>,现给你一个对应的查询数组&nbsp;<code>queries</code>,其中&nbsp;<code>queries[i] = [L<sub>i,&nbsp;</sub>R<sub>i</sub>]</code></p>
<p>对于每个查询&nbsp;<code>i</code>,请你计算从&nbsp;<code>L<sub>i</sub></code>&nbsp;&nbsp;<code>R<sub>i</sub></code>&nbsp;&nbsp;<strong>XOR</strong>&nbsp;值(即&nbsp;<code>arr[L<sub>i</sub>] <strong>xor</strong> arr[L<sub>i+1</sub>] <strong>xor</strong> ... <strong>xor</strong> arr[R<sub>i</sub>]</code>)作为本次查询的结果。</p>
<p>并返回一个包含给定查询&nbsp;<code>queries</code>&nbsp;所有结果的数组。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>输出:</strong>[2,7,14,8]
<strong>解释:</strong>
数组中元素的二进制表示形式是:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
查询的 XOR 值为:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[3,3] = 8
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
<strong>输出:</strong>[8,0,4,4]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= arr.length &lt;= 3 *&nbsp;10^4</code></li>
<li><code>1 &lt;= arr[i] &lt;= 10^9</code></li>
<li><code>1 &lt;= queries.length &lt;= 3 * 10^4</code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 &lt;= queries[i][0] &lt;= queries[i][1] &lt; arr.length</code></li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 112</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long