Skip to content

Commit 7e0d4d5

Browse files
committed
Refine 1310_XOR_Queries_of_a_Subarray
1 parent 7ffaa0d commit 7e0d4d5

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
228228
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) |
229229
| 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py) | Take 2 to the power digit position from right (starting from 0) and multiply it with the digit |
230230
| 1304 | [Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1304_Find_N_Unique_Integers_Sum_up_to_Zero.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1304_Find_N_Unique_Integers_Sum_up_to_Zero.java) | [1,n-1] and its negative sum |
231+
| 1310 | [XOR Queries of a Subarray](https://leetcode.com/problems/xor-queries-of-a-subarray) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1310_XOR_Queries_of_a_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1310_XOR_Queries_of_a_Subarray.java) [Cpp](https://github.com/qiyuangong/leetcode/blob/master/cpp/1310_XOR_Queries_of_a_Subarray.cpp) | Compute accumulated xor from head, qeury result equals to xor[0, l] xor x[0, r], O(n) and O(n) |
231232
| 1323 | [Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1323_Maximum_69_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1323_Maximum_69_Number.java) | 9 is greater than 6, so change first 6 to 9 from left if exist, O(n) and O(1) |
232233
| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) |
233234
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py) | If number is divisible by 2, divide the number by 2, else subtract 1 from the number, and output the number of steps, O(logn) and O(1) |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
4+
vector<int> res;
5+
// Compute accumulated xor from head
6+
for (int i = 1; i < arr.size(); i++)
7+
arr[i] ^= arr[i - 1];
8+
for (auto &q: queries)
9+
res.push_back(q[0] > 0 ? arr[q[0] - 1] ^ arr[q[1]] : arr[q[1]]);
10+
return res;
11+
}
12+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int[] xorQueries(int[] arr, int[][] queries) {
3+
int[] res = new int[queries.length], q;
4+
// Compute accumulated xor from head
5+
for (int i = 1; i < arr.length; i++)
6+
arr[i] ^= arr[i - 1];
7+
// query result equals to xor[0, l] xor xor[0, r]
8+
for (int i = 0; i < queries.length; i++) {
9+
q = queries[i];
10+
res[i] = q[0] > 0 ? arr[q[0] - 1] ^ arr[q[1]] : arr[q[1]];
11+
}
12+
return res;
13+
}
14+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
class Solution:
22
def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
33
pref = [0]
4+
# Compute accumulated xor from head
45
for e in arr:
56
pref.append(e ^ pref[-1])
67
ans = []
8+
# query result equal to xor[0, l] xor x[0, r]
79
for [l, r] in queries:
810
ans.append(pref[r+1] ^ pref[l])
911
return ans
10-
12+
13+
# def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
14+
# for i in range(len(arr) - 1):
15+
# arr[i + 1] ^= arr[i]
16+
# return [arr[j] ^ arr[i - 1] if i else arr[j] for i, j in queries]

0 commit comments

Comments
 (0)