1310. XOR Queries of a Subarray
package LeetCode_1310 /** * 1310. XOR Queries of a Subarray * https://leetcode.com/problems/xor-queries-of-a-subarray/description/ * * Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], * for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor ... xor arr[Ri] ). * Return an array containing the result for the given queries. Example 1: Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]] Output: [2,7,14,8] Explanation: The binary representation of the elements in the array are: 1 = 0001 3 = 0011 4 = 0100 8 = 1000 The XOR values for queries are: [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 Example 2: Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]] Output: [8,0,4,4] Constraints: 1. 1 <= arr.length <= 3 * 10^4 2. 1 <= arr[i] <= 10^9 3. 1 <= queries.length <= 3 * 10^4 4. queries[i].length == 2 5. 0 <= queries[i][0] <= queries[i][1] < arr.length * */ class Solution { /* * solution: prefix xor array, * because a^b^a = b, q[l,r] = q[0,r] ^ q[0,l-1], so q[l,r] = prefixXOR[r+1] ^ prefixXOR[l] * Time complexity:O(n)+O(q), Space complexity:O(n) * */ fun xorQueries(arr: IntArray, queries: Array<IntArray>): IntArray? { val n = arr.size val prefixXOR = IntArray(n + 1) for (i in 0 until n) { prefixXOR[i + 1] = prefixXOR[i] xor arr[i] } val result = IntArray(queries.size) for (i in queries.indices) { val left = queries[i][0] val right = queries[i][1] result[i] = prefixXOR[right + 1] xor prefixXOR[left] } return result } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)