当前位置:网站首页>Leecode subarray XOR query
Leecode subarray XOR query
2022-07-18 08:25:00 【Java Genie】
There's an array of positive integers arr, Now I give you a corresponding query array queries, among queries[i] = [Li, Ri].
For each query i, Please calculate from Li To Ri Of XOR value ( namely arr[Li] xor arr[Li+1] xor … xor arr[Ri]) As a result of this query .
And returns a query containing the given queries An array of all the results .
Example 1:
Input :arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
Output :[2,7,14,8]
explain :
The binary representation of the elements in an array is :
1 = 0001
3 = 0011
4 = 0100
8 = 1000
Of the query XOR The value is :
[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]
Tips :
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
Ideas :
1、queries The first and second elements of each row in the array of , Namely arr The left and right bounds of the array
2、 stay queries Take the first and second numbers of a row of data in the array as the left and right bounds , Starting from the left boundary, it is connected with the next number in turn, or , Give the result to ans In the array
3、 return ans
Implementation code :
/* The following code is my local Idea For testing in environment public class XorSelect { public static void main(String[] args) { int arr[]={4,8,2,10}; int [][]q={
{2,3},{1,3},{0,0},{0,3}}; int[] ints = new Solution().xorQueries(arr, q); } } */
class Solution{
public int[] xorQueries(int[] arr, int[][] queries) {
int []ans=new int[queries.length];
for (int i = 0; i < queries.length; i++) {
int l=queries[i][0];
int r=queries[i][1];
int res=arr[l];
for (int j=l;j<r;j++){
res=res^arr[j+1];
}
/* if (i!= queries.length-1)System.out.print(res+" " ); else System.out.println(res); */
ans[i]=res;
}
return ans ;
}
}
边栏推荐
- SAP S/4HANA New Simplified Data Model (NSDM) 模型介绍
- Torch in pytoch Max() function analysis
- Matlab calculates the integral of normal function, and the quantile corresponding to the integral
- Li Mu hands on deep learning V2 target detection data set
- R language uses the melt function of reshape2 package to transform dataframe data from wide table to long table, and dcast function processes the data processed by melt function from long table to wid
- Matlab:trainingoptions() explain training options in detail
- SAR图像:拟合杂波时常用的分布
- Common differences between MySQL and Oracle (I)
- Anhui University store
- 990. Satisfiability of equation · union search set
猜你喜欢

The space dream of billionaires is killing the earth

338. Bit counting · dynamic programming

How to set the allure test report

676.实现一个魔法字典·前缀树

竞赛·6116·计算布尔二叉树的值·递归

406·根据身高重建队列·贪心

Matlab:搭建神经网络

Data transmission: Practice of batch extraction of isomorphic and heterogeneous IP data sources

每日一题·648.单词替换·前缀树

薄膜铜箔导电电位测量
随机推荐
Torch in pytoch Sort() and torch Argsort() function parsing
Several things I want to understand about being laid off at the age of 30
Race · 6116 · calculate the value of Boolean binary tree · recursion
C language · prefix tree implementation
Broadcast mechanism in pytoch
Broadcast mechanism in pytoch
测试光流传感器速度特性
SAP S/4HANA New Simplified Data Model (NSDM) 模型介绍
[untitled] slow SQL analysis and optimization
Torch in pytoch full(),torch. Ones() and torch Analysis of zeros() function
C语言·前缀树实现
Matlab:图像增强 imageDataAugmenter() 的用法
开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
薄膜铜箔导电电位测量
Meituan side: @transactional principle and common pits?
競賽·6116·計算布爾二叉樹的值·遞歸
Common differences between MySQL and Oracle (I)
Software architecture and design (IX) -- component based architecture
Is it safe for tongdaxin to open an account? Which securities are good for opening an account
Torch in pytoch repeat_ Analysis of interleave() function