GeezerVison is showing github.com — Refine 1310_XOR_Queries_of_a_Subarray · pengaoao/leetcode@7e0d4d5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -228,6 +228,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 228 | 228 | | 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) | | 229 | 229 | | 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 | | 230 | 230 | | 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) | | 231 | 232 | | 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) | | 232 | 233 | | 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) | | 233 | 234 | | 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) | | |
Collapse file Copy file name to clipboard +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +}; |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboard +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | class Solution: | 2 | 2 | def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: | 3 | 3 | pref = [0] | | 4 | + # Compute accumulated xor from head | 4 | 5 | for e in arr: | 5 | 6 | pref.append(e ^ pref[-1]) | 6 | 7 | ans = [] | | 8 | + # query result equal to xor[0, l] xor x[0, r] | 7 | 9 | for [l, r] in queries: | 8 | 10 | ans.append(pref[r+1] ^ pref[l]) | 9 | 11 | 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] |
|