Skip to content

feat: add solutions to lc problem: No.1311 #3406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions solution/1300-1399/1310.XOR Queries of a Subarray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ tags:

<pre>
<strong>输入:</strong>arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>输出:</strong>[2,7,14,8]
<strong>输出:</strong>[2,7,14,8]
<strong>解释:</strong>
数组中元素的二进制表示形式是:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
1 = 0001
3 = 0011
4 = 0100
8 = 1000
查询的 XOR 值为:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[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
</pre>

Expand Down Expand Up @@ -73,18 +73,18 @@ tags:

### 方法一:前缀异或

我们可以用一个长度为 $n+1$ 的前缀异或数组 $s$ 来存储数组 $arr$ 的前缀异或结果,其中 $s[i] = s[i-1] \oplus arr[i-1]$,即 $s[i]$ 表示 $arr$ 中下标 $[0,i-1]$ 的元素的异或结果
我们可以用一个长度为 $n+1$ 的前缀异或数组 $s$ 来存储数组 $\textit{arr}$ 的前缀异或结果,其中 $s[i] = s[i-1] \oplus \textit{arr}[i-1]$,即 $s[i]$ 表示 $\textit{arr}$ 的前 $i$ 个元素的异或结果

那么对于一个查询 $[l,r]$,我们可以得到:

$$
\begin{aligned}
arr[l] \oplus arr[l+1] \oplus \cdots \oplus arr[r] &= (arr[0] \oplus arr[1] \oplus \cdots \oplus arr[l-1]) \oplus (arr[0] \oplus arr[1] \oplus \cdots \oplus arr[r]) \\
\textit{arr}[l] \oplus \textit{arr}[l+1] \oplus \cdots \oplus \textit{arr}[r] &= (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[l-1]) \oplus (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[r]) \\
&= s[l] \oplus s[r+1]
\end{aligned}
$$

时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $arr$ 和查询数组 $queries$ 的长度。
时间复杂度 $O(n+m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 $\textit{arr}$ 的长度和查询数组 $\textit{queries}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -162,15 +162,11 @@ func xorQueries(arr []int, queries [][]int) (ans []int) {
```ts
function xorQueries(arr: number[], queries: number[][]): number[] {
const n = arr.length;
const s: number[] = new Array(n + 1).fill(0);
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans: number[] = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
}
```

Expand All @@ -184,15 +180,11 @@ function xorQueries(arr: number[], queries: number[][]): number[] {
*/
var xorQueries = function (arr, queries) {
const n = arr.length;
const s = new Array(n + 1).fill(0);
const s = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
};
```

Expand Down
49 changes: 27 additions & 22 deletions solution/1300-1399/1310.XOR Queries of a Subarray/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ tags:

<pre>
<strong>Input:</strong> arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
<strong>Output:</strong> [2,7,14,8]
<strong>Explanation:</strong>
The binary representation of the elements in the array are:
1 = 0001
3 = 0011
4 = 0100
8 = 1000
1 = 0001
3 = 0011
4 = 0100
8 = 1000
The XOR values for queries are:
[0,1] = 1 xor 3 = 2
[1,2] = 3 xor 4 = 7
[0,3] = 1 xor 3 xor 4 xor 8 = 14
[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
</pre>

Expand All @@ -68,7 +68,20 @@ The XOR values for queries are:

<!-- solution:start -->

### Solution 1
### Solution 1: Prefix XOR

We can use a prefix XOR array $s$ of length $n+1$ to store the prefix XOR results of the array $\textit{arr}$, where $s[i] = s[i-1] \oplus \textit{arr}[i-1]$. That is, $s[i]$ represents the XOR result of the first $i$ elements of $\textit{arr}$.

For a query $[l, r]$, we can obtain:

$$
\begin{aligned}
\textit{arr}[l] \oplus \textit{arr}[l+1] \oplus \cdots \oplus \textit{arr}[r] &= (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[l-1]) \oplus (\textit{arr}[0] \oplus \textit{arr}[1] \oplus \cdots \oplus \textit{arr}[r]) \\
&= s[l] \oplus s[r+1]
\end{aligned}
$$

Time complexity is $O(n+m)$, and space complexity is $O(n)$. Here, $n$ and $m$ are the lengths of the array $\textit{arr}$ and the query array $\textit{queries}$, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -146,15 +159,11 @@ func xorQueries(arr []int, queries [][]int) (ans []int) {
```ts
function xorQueries(arr: number[], queries: number[][]): number[] {
const n = arr.length;
const s: number[] = new Array(n + 1).fill(0);
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans: number[] = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
}
```

Expand All @@ -168,15 +177,11 @@ function xorQueries(arr: number[], queries: number[][]): number[] {
*/
var xorQueries = function (arr, queries) {
const n = arr.length;
const s = new Array(n + 1).fill(0);
const s = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
};
```

Expand Down
8 changes: 2 additions & 6 deletions solution/1300-1399/1310.XOR Queries of a Subarray/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
*/
var xorQueries = function (arr, queries) {
const n = arr.length;
const s = new Array(n + 1).fill(0);
const s = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
};
8 changes: 2 additions & 6 deletions solution/1300-1399/1310.XOR Queries of a Subarray/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
function xorQueries(arr: number[], queries: number[][]): number[] {
const n = arr.length;
const s: number[] = new Array(n + 1).fill(0);
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] ^ arr[i];
}
const ans: number[] = [];
for (const [l, r] of queries) {
ans.push(s[r + 1] ^ s[l]);
}
return ans;
return queries.map(([l, r]) => s[r + 1] ^ s[l]);
}
Loading
Loading