Skip to content

Commit e3304f5

Browse files
committed
Add solution 1310
1 parent 012999a commit e3304f5

28 files changed

+715
-490
lines changed

README.md

Lines changed: 350 additions & 350 deletions
Large diffs are not rendered by default.

leetcode/0304.Range-Sum-Query-2D-Immutable/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
77

8-
![https://leetcode.com/static/images/courses/range_sum_query_2d.png](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
8+
![https://leetcode.com/static/images/courses/range_sum_query_2d.png](https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg)
99

1010
The above rectangle (with the red border) is defined by (row1, col1) = **(2, 1)** and (row2, col2) = **(4, 3)**, which contains sum = **8**.
1111

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode
2+
3+
func xorQueries(arr []int, queries [][]int) []int {
4+
xors := make([]int, len(arr))
5+
xors[0] = arr[0]
6+
for i := 1; i < len(arr); i++ {
7+
xors[i] = arr[i] ^ xors[i-1]
8+
}
9+
res := make([]int, len(queries))
10+
for i, q := range queries {
11+
res[i] = xors[q[1]]
12+
if q[0] > 0 {
13+
res[i] ^= xors[q[0]-1]
14+
}
15+
}
16+
return res
17+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1310 struct {
9+
para1310
10+
ans1310
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1310 struct {
16+
arr []int
17+
queries [][]int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1310 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem1310(t *testing.T) {
27+
28+
qs := []question1310{
29+
30+
{
31+
para1310{[]int{1, 3, 4, 8}, [][]int{{0, 1}, {1, 2}, {0, 3}, {3, 3}}},
32+
ans1310{[]int{2, 7, 14, 8}},
33+
},
34+
35+
{
36+
para1310{[]int{4, 8, 2, 10}, [][]int{{2, 3}, {1, 3}, {0, 0}, {0, 3}}},
37+
ans1310{[]int{8, 0, 4, 4}},
38+
},
39+
}
40+
41+
fmt.Printf("------------------------Leetcode Problem 1310------------------------\n")
42+
43+
for _, q := range qs {
44+
_, p := q.ans1310, q.para1310
45+
fmt.Printf("【input】:%v 【output】:%v\n", p, xorQueries(p.arr, p.queries))
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# [1310. XOR Queries of a Subarray](https://leetcode.com/problems/xor-queries-of-a-subarray/)
2+
3+
4+
## 题目
5+
6+
Given the array `arr` of positive integers and the array `queries` where `queries[i] = [Li,Ri]`, for each query `i` compute the **XOR** of elements from `Li` to `Ri` (that is, `arr[Li]xor arr[Li+1]xor ...xor arr[Ri]`). Return an array containing the result for the given `queries`.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
12+
Output: [2,7,14,8]
13+
Explanation:
14+
The binary representation of the elements in the array are:
15+
1 = 0001
16+
3 = 0011
17+
4 = 0100
18+
8 = 1000
19+
The XOR values for queries are:
20+
[0,1] = 1 xor 3 = 2
21+
[1,2] = 3 xor 4 = 7
22+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
23+
[3,3] = 8
24+
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
31+
Output: [8,0,4,4]
32+
33+
```
34+
35+
**Constraints:**
36+
37+
- `1 <= arr.length <= 3 * 10^4`
38+
- `1 <= arr[i] <= 10^9`
39+
- `1 <= queries.length <= 3 * 10^4`
40+
- `queries[i].length == 2`
41+
- `0 <= queries[i][0] <= queries[i][1] < arr.length`
42+
43+
## 题目大意
44+
45+
有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]。对于每个查询 i,请你计算从 Li 到 Ri 的 XOR 值(即 arr[Li] xor arr[Li+1] xor ... xor arr[Ri])作为本次查询的结果。并返回一个包含给定查询 queries 所有结果的数组。
46+
47+
## 解题思路
48+
49+
- 此题求区间异或,很容易让人联想到区间求和。区间求和利用前缀和,可以使得 query 从 O(n) 降为 O(1)。区间异或能否也用类似前缀和的思想呢?答案是肯定的。利用异或的两个性质,x ^ x = 0,x ^ 0 = x。那么有:(由于 LaTeX 中异或符号 ^ 是特殊字符,笔者用 $\oplus$ 代替异或)
50+
51+
$$\begin{aligned}Query(left,right) &=arr[left] \oplus \cdots  \oplus arr[right]\\&=(arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[left] \oplus \cdots  \oplus arr[right])\\ &=(arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[0] \oplus \cdots  \oplus arr[right])\\ &=xors[left] \oplus xors[right+1]\\ \end{aligned}$$
52+
53+
按照这个思路解题,便可以将 query 从 O(n) 降为 O(1),总的时间复杂度为 O(n)。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func xorQueries(arr []int, queries [][]int) []int {
61+
xors := make([]int, len(arr))
62+
xors[0] = arr[0]
63+
for i := 1; i < len(arr); i++ {
64+
xors[i] = arr[i] ^ xors[i-1]
65+
}
66+
res := make([]int, len(queries))
67+
for i, q := range queries {
68+
res[i] = xors[q[1]]
69+
if q[0] > 0 {
70+
res[i] ^= xors[q[0]-1]
71+
}
72+
}
73+
return res
74+
}
75+
```

website/content/ChapterFour/0300~0399/0304.Range-Sum-Query-2D-Immutable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
77

8-
![https://leetcode.com/static/images/courses/range_sum_query_2d.png](https://leetcode.com/static/images/courses/range_sum_query_2d.png)
8+
![https://leetcode.com/static/images/courses/range_sum_query_2d.png](https://assets.leetcode.com/uploads/2021/03/14/sum-grid.jpg)
99

1010
The above rectangle (with the red border) is defined by (row1, col1) = **(2, 1)** and (row2, col2) = **(4, 3)**, which contains sum = **8**.
1111

website/content/ChapterFour/1300~1399/1306.Jump-Game-III.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ func canReach(arr []int, start int) bool {
8383
----------------------------------------------
8484
<div style="display: flex;justify-content: space-between;align-items: center;">
8585
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1305.All-Elements-in-Two-Binary-Search-Trees/">⬅️上一页</a></p>
86-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1313.Decompress-Run-Length-Encoded-List/">下一页➡️</a></p>
86+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1310.XOR-Queries-of-a-Subarray/">下一页➡️</a></p>
8787
</div>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [1310. XOR Queries of a Subarray](https://leetcode.com/problems/xor-queries-of-a-subarray/)
2+
3+
4+
## 题目
5+
6+
Given the array `arr` of positive integers and the array `queries` where `queries[i] = [Li,Ri]`, for each query `i` compute the **XOR** of elements from `Li` to `Ri` (that is, `arr[Li]xor arr[Li+1]xor ...xor arr[Ri]`). Return an array containing the result for the given `queries`.
7+
8+
**Example 1:**
9+
10+
```
11+
Input: arr = [1,3,4,8], queries = [[0,1],[1,2],[0,3],[3,3]]
12+
Output: [2,7,14,8]
13+
Explanation:
14+
The binary representation of the elements in the array are:
15+
1 = 0001
16+
3 = 0011
17+
4 = 0100
18+
8 = 1000
19+
The XOR values for queries are:
20+
[0,1] = 1 xor 3 = 2
21+
[1,2] = 3 xor 4 = 7
22+
[0,3] = 1 xor 3 xor 4 xor 8 = 14
23+
[3,3] = 8
24+
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: arr = [4,8,2,10], queries = [[2,3],[1,3],[0,0],[0,3]]
31+
Output: [8,0,4,4]
32+
33+
```
34+
35+
**Constraints:**
36+
37+
- `1 <= arr.length <= 3 * 10^4`
38+
- `1 <= arr[i] <= 10^9`
39+
- `1 <= queries.length <= 3 * 10^4`
40+
- `queries[i].length == 2`
41+
- `0 <= queries[i][0] <= queries[i][1] < arr.length`
42+
43+
## 题目大意
44+
45+
有一个正整数数组 arr,现给你一个对应的查询数组 queries,其中 queries[i] = [Li, Ri]。对于每个查询 i,请你计算从 Li 到 Ri 的 XOR 值(即 arr[Li] xor arr[Li+1] xor ... xor arr[Ri])作为本次查询的结果。并返回一个包含给定查询 queries 所有结果的数组。
46+
47+
## 解题思路
48+
49+
- 此题求区间异或,很容易让人联想到区间求和。区间求和利用前缀和,可以使得 query 从 O(n) 降为 O(1)。区间异或能否也用类似前缀和的思想呢?答案是肯定的。利用异或的两个性质,x ^ x = 0,x ^ 0 = x。那么有:(由于 LaTeX 中异或符号 ^ 是特殊字符,笔者用 {{< katex >}} \oplus {{< /katex >}} 代替异或)
50+
51+
{{< katex display >}}
52+
\begin{aligned}Query(left,right) &=arr[left] \oplus \cdots  \oplus arr[right]\\&=(arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[left] \oplus \cdots  \oplus arr[right])\\ &=(arr[0] \oplus \cdots  \oplus arr[left-1]) \oplus (arr[0] \oplus \cdots  \oplus arr[right])\\ &=xors[left] \oplus xors[right+1]\\ \end{aligned}
53+
{{< /katex >}}
54+
55+
按照这个思路解题,便可以将 query 从 O(n) 降为 O(1),总的时间复杂度为 O(n)。
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func xorQueries(arr []int, queries [][]int) []int {
63+
xors := make([]int, len(arr))
64+
xors[0] = arr[0]
65+
for i := 1; i < len(arr); i++ {
66+
xors[i] = arr[i] ^ xors[i-1]
67+
}
68+
res := make([]int, len(queries))
69+
for i, q := range queries {
70+
res[i] = xors[q[1]]
71+
if q[0] > 0 {
72+
res[i] ^= xors[q[0]-1]
73+
}
74+
}
75+
return res
76+
}
77+
```
78+
79+
80+
----------------------------------------------
81+
<div style="display: flex;justify-content: space-between;align-items: center;">
82+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1306.Jump-Game-III/">⬅️上一页</a></p>
83+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1313.Decompress-Run-Length-Encoded-List/">下一页➡️</a></p>
84+
</div>

website/content/ChapterFour/1300~1399/1313.Decompress-Run-Length-Encoded-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ func decompressRLElist(nums []int) []int {
6262

6363
----------------------------------------------
6464
<div style="display: flex;justify-content: space-between;align-items: center;">
65-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1306.Jump-Game-III/">⬅️上一页</a></p>
65+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1310.XOR-Queries-of-a-Subarray/">⬅️上一页</a></p>
6666
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1300~1399/1317.Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/">下一页➡️</a></p>
6767
</div>

0 commit comments

Comments
 (0)