leet-code/src/main/java/leetcode/editor/cn/XorQueriesOfASubarray.java
2021-05-12 22:56:42 +08:00

72 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//有一个正整数数组 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)
}