Skip to content

Commit 5f6ea09

Browse files
committed
Add solution 1486
1 parent 2a115ec commit 5f6ea09

26 files changed

+990
-774
lines changed

README.md

Lines changed: 535 additions & 533 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package leetcode
2+
3+
func xorOperation(n int, start int) int {
4+
res := 0
5+
for i := 0; i < n; i++ {
6+
res ^= start + 2*i
7+
}
8+
return res
9+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1486 struct {
9+
para1486
10+
ans1486
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1486 struct {
16+
n int
17+
start int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1486 struct {
23+
one int
24+
}
25+
26+
func Test_Problem1486(t *testing.T) {
27+
28+
qs := []question1486{
29+
30+
{
31+
para1486{5, 0},
32+
ans1486{8},
33+
},
34+
35+
{
36+
para1486{4, 3},
37+
ans1486{8},
38+
},
39+
40+
{
41+
para1486{1, 7},
42+
ans1486{7},
43+
},
44+
45+
{
46+
para1486{10, 5},
47+
ans1486{2},
48+
},
49+
}
50+
51+
fmt.Printf("------------------------Leetcode Problem 1486------------------------\n")
52+
53+
for _, q := range qs {
54+
_, p := q.ans1486, q.para1486
55+
fmt.Printf("【input】:%v 【output】:%v \n", p, xorOperation(p.n, p.start))
56+
}
57+
fmt.Printf("\n\n\n")
58+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [1486. XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)
2+
3+
4+
## 题目
5+
6+
Given an integer `n` and an integer `start`.
7+
8+
Define an array `nums` where `nums[i] = start + 2*i` (0-indexed) and `n == nums.length`.
9+
10+
Return the bitwise XOR of all elements of `nums`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: n = 5, start = 0
16+
Output: 8
17+
Explanation:Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
18+
Where "^" corresponds to bitwise XOR operator.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: n = 4, start = 3
25+
Output: 8
26+
Explanation:Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
27+
```
28+
29+
**Example 3:**
30+
31+
```
32+
Input: n = 1, start = 7
33+
Output: 7
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: n = 10, start = 5
40+
Output: 2
41+
```
42+
43+
**Constraints:**
44+
45+
- `1 <= n <= 1000`
46+
- `0 <= start <= 1000`
47+
- `n == nums.length`
48+
49+
## 题目大意
50+
51+
给你两个整数,n 和 start 。数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。请返回 nums 中所有元素按位异或(XOR)后得到的结果。
52+
53+
## 解题思路
54+
55+
- 简单题。按照题意,一层循环依次累积异或数组中每个元素。
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func xorOperation(n int, start int) int {
63+
res := 0
64+
for i := 0; i < n; i++ {
65+
res ^= start + 2*i
66+
}
67+
return res
68+
}
69+
```

website/content/ChapterFour/1400~1499/1480.Running-Sum-of-1d-Array.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ func runningSum(nums []int) []int {
6767
----------------------------------------------
6868
<div style="display: flex;justify-content: space-between;align-items: center;">
6969
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1470.Shuffle-the-Array/">⬅️上一页</a></p>
70-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">下一页➡️</a></p>
70+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array/">下一页➡️</a></p>
7171
</div>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [1486. XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)
2+
3+
4+
## 题目
5+
6+
Given an integer `n` and an integer `start`.
7+
8+
Define an array `nums` where `nums[i] = start + 2*i` (0-indexed) and `n == nums.length`.
9+
10+
Return the bitwise XOR of all elements of `nums`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: n = 5, start = 0
16+
Output: 8
17+
Explanation:Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ 6 ^ 8) = 8.
18+
Where "^" corresponds to bitwise XOR operator.
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: n = 4, start = 3
25+
Output: 8
26+
Explanation:Array nums is equal to [3, 5, 7, 9] where (3 ^ 5 ^ 7 ^ 9) = 8.
27+
```
28+
29+
**Example 3:**
30+
31+
```
32+
Input: n = 1, start = 7
33+
Output: 7
34+
```
35+
36+
**Example 4:**
37+
38+
```
39+
Input: n = 10, start = 5
40+
Output: 2
41+
```
42+
43+
**Constraints:**
44+
45+
- `1 <= n <= 1000`
46+
- `0 <= start <= 1000`
47+
- `n == nums.length`
48+
49+
## 题目大意
50+
51+
给你两个整数,n 和 start 。数组 nums 定义为:nums[i] = start + 2*i(下标从 0 开始)且 n == nums.length 。请返回 nums 中所有元素按位异或(XOR)后得到的结果。
52+
53+
## 解题思路
54+
55+
- 简单题。按照题意,一层循环依次累积异或数组中每个元素。
56+
57+
## 代码
58+
59+
```go
60+
package leetcode
61+
62+
func xorOperation(n int, start int) int {
63+
res := 0
64+
for i := 0; i < n; i++ {
65+
res ^= start + 2*i
66+
}
67+
return res
68+
}
69+
```
70+
71+
72+
----------------------------------------------
73+
<div style="display: flex;justify-content: space-between;align-items: center;">
74+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1480.Running-Sum-of-1d-Array/">⬅️上一页</a></p>
75+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1512.Number-of-Good-Pairs/">下一页➡️</a></p>
76+
</div>

website/content/ChapterFour/1500~1599/1512.Number-of-Good-Pairs.md

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

7070
----------------------------------------------
7171
<div style="display: flex;justify-content: space-between;align-items: center;">
72-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1480.Running-Sum-of-1d-Array/">⬅️上一页</a></p>
72+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1400~1499/1486.XOR-Operation-in-an-Array/">⬅️上一页</a></p>
7373
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1500~1599/1539.Kth-Missing-Positive-Number/">下一页➡️</a></p>
7474
</div>

0 commit comments

Comments
 (0)