当前位置:网站首页>LetCode 1829. Maximum XOR value per query
LetCode 1829. Maximum XOR value per query
2022-07-01 03:52:00 【Daylight629】
1829. The maximum XOR value of each query
To give you one Orderly Array nums
, It consists of n
It's a nonnegative integer , I'll give you an integer at the same time maximumBit
. You need to perform the following query n
Time :
- Find a nonnegative integer
k < 2maximumBit
, bringnums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k
Result Maximize .k
It's Noi
The answer to a query . - From the current array
nums
Delete Last An element .
Please return an array answer
, among answer[i]
It's No i
The result of a query .
Example 1:
Input :nums = [0,1,1,3], maximumBit = 2
Output :[0,3,2,3]
explain : The answers to the query are as follows :
First query :nums = [0,1,1,3],k = 0, because 0 XOR 1 XOR 1 XOR 3 XOR 0 = 3 .
The second query :nums = [0,1,1],k = 3, because 0 XOR 1 XOR 1 XOR 3 = 3 .
The third query :nums = [0,1],k = 2, because 0 XOR 1 XOR 2 = 3 .
The fourth query :nums = [0],k = 3, because 0 XOR 3 = 3 .
Example 2:
Input :nums = [2,3,4,7], maximumBit = 3
Output :[5,2,6,5]
explain : The answers to the query are as follows :
First query :nums = [2,3,4,7],k = 5, because 2 XOR 3 XOR 4 XOR 7 XOR 5 = 7.
The second query :nums = [2,3,4],k = 2, because 2 XOR 3 XOR 4 XOR 2 = 7 .
The third query :nums = [2,3],k = 6, because 2 XOR 3 XOR 6 = 7 .
The fourth query :nums = [2],k = 5, because 2 XOR 5 = 7 .
Example 3:
Input :nums = [0,1,2,2,5,7], maximumBit = 3
Output :[4,3,6,4,6,7]
Tips :
nums.length == n
1 <= n <= 105
1 <= maximumBit <= 20
0 <= nums[i] < 2maximumBit
nums
The number in has been pressed Ascending Arrange order well .
Two 、 Method 1
An operation
class Solution {
public int[] getMaximumXor(int[] nums, int maximumBit) {
int t = (1 << maximumBit)-1;
int n = nums.length;
int[] ans = new int[n];
ans[n-1] = nums[0] ^ t;
for(int i = 1; i < n; i++){
nums[i] ^= nums[i-1];
ans[n-1-i] = nums[i] ^ t;
}
return ans;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(1). The space required to store the returned answer is not included here .
边栏推荐
- Download and installation configuration of cygwin
- Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
- C语言的sem_t变量类型
- Blueprism registration, download and install -rpa Chapter 1
- 205. isomorphic string
- 166. fractions to decimals
- 【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
- Unexpected token o in JSON at position 1, JSON parsing problem
- 165. 比较版本号
- Deep learning | rnn/lstm of naturallanguageprocessing
猜你喜欢
Future of NTF and trends in 2022
【TA-霜狼_may-《百人计划》】2.1 色彩空间
Binary tree god level traversal: Morris traversal
SEM of C language_ Tvariable type
谷粒学院微信扫码登录过程记录以及bug解决
【TA-霜狼_may-《百人计划》】1.3纹理的秘密
TS type gymnastics: illustrating a complex advanced type
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture
431. 将 N 叉树编码为二叉树 DFS
Download and installation configuration of cygwin
随机推荐
208. 实现 Trie (前缀树)
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
25.K个一组翻转链表
[reach out to Party welfare] developer reload system sequence
在 C 中声明函数之前调用函数会发生什么?
【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算
5. [WebGIS practice] software operation - service release and permission management
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
10. 正则表达式匹配
10、Scanner. Next() cannot read spaces /indexof -1
The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)
这可能是你进腾讯最后的机会了..
206.反转链表
242. valid Letter heteronyms
Binary tree god level traversal: Morris traversal
【TA-霜狼_may-《百人计划》】2.3 常用函数介绍
Processing of menu buttons on the left and contents on the right of the background system page, and double scrolling appears on the background system page
187. 重复的DNA序列
431. encode n-ary tree as binary tree DFS