作者dont (dont)
標題Re: [閒聊] 每日leetcode
時間2024-09-13 12:48:29
1310. XOR Queries of a Subarray
## 思路
先建PrefixSum的表, 再query差值
ex.
arr = [a1, a2, a3, ...]
prefix = [0, a1, a1^a2, a1^a2^a3, ...]
a2^a3 = (a1^a2^a3) ^ a1
## Code
```python
class Solution:
def xorQueries(self, arr: List[int], queries: List[List[int]]) ->
List[int]:
prefix = [0]
for num in arr:
prefix.append(prefix[-1] ^ num)
res = []
for left, right in queries:
res.append(prefix[right+1] ^ prefix[left])
return res
```
--
https://i.imgur.com/kyBhy6o.jpeg
--
※ 發信站: 批踢踢實業坊(web-ptt.tw), 來自: 185.213.82.238 (臺灣)
※ 文章網址: https://web-ptt.tw/Marginalman/M.1726202917.A.F41