Skip to content

Commit 0b97bf6

Browse files
committed
feat: add solutions to lc problem: No.1486
No.1486.XOR Operation in an Array
1 parent 09ac186 commit 0b97bf6

File tree

7 files changed

+96
-22
lines changed

7 files changed

+96
-22
lines changed

solution/1400-1499/1486.XOR Operation in an Array/README.md

+39-7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
**方法一:模拟**
58+
59+
我们可以直接模拟算出数组中所有元素的异或结果。
60+
61+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组长度。
62+
5763
<!-- tabs:start -->
5864

5965
### **Python3**
@@ -63,10 +69,10 @@
6369
```python
6470
class Solution:
6571
def xorOperation(self, n: int, start: int) -> int:
66-
res = 0
72+
ans = 0
6773
for i in range(n):
68-
res ^= start + (i << 1)
69-
return res
74+
ans ^= start + 2 * i
75+
return ans
7076
```
7177

7278
### **Java**
@@ -76,15 +82,41 @@ class Solution:
7682
```java
7783
class Solution {
7884
public int xorOperation(int n, int start) {
79-
int ret = start;
80-
for (int i = 1; i < n; i++) {
81-
ret = ret ^ (start + (i << 1));
85+
int ans = 0;
86+
for (int i = 0; i < n; ++i) {
87+
ans ^= start + 2 * i;
8288
}
83-
return ret;
89+
return ans;
8490
}
8591
}
8692
```
8793

94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
int xorOperation(int n, int start) {
100+
int ans = 0;
101+
for (int i = 0; i < n; ++i) {
102+
ans ^= start + 2 * i;
103+
}
104+
return ans;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func xorOperation(n int, start int) (ans int) {
113+
for i := 0; i < n; i++ {
114+
ans ^= start + 2*i
115+
}
116+
return
117+
}
118+
```
119+
88120
### **...**
89121

90122
```

solution/1400-1499/1486.XOR Operation in an Array/README_EN.md

+33-7
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,52 @@ Where &quot;^&quot; corresponds to bitwise XOR operator.
4646
```python
4747
class Solution:
4848
def xorOperation(self, n: int, start: int) -> int:
49-
res = 0
49+
ans = 0
5050
for i in range(n):
51-
res ^= start + (i << 1)
52-
return res
51+
ans ^= start + 2 * i
52+
return ans
5353
```
5454

5555
### **Java**
5656

5757
```java
5858
class Solution {
5959
public int xorOperation(int n, int start) {
60-
int ret = start;
61-
for (int i = 1; i < n; i++) {
62-
ret = ret ^ (start + (i << 1));
60+
int ans = 0;
61+
for (int i = 0; i < n; ++i) {
62+
ans ^= start + 2 * i;
6363
}
64-
return ret;
64+
return ans;
6565
}
6666
}
6767
```
6868

69+
### **C++**
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
int xorOperation(int n, int start) {
75+
int ans = 0;
76+
for (int i = 0; i < n; ++i) {
77+
ans ^= start + 2 * i;
78+
}
79+
return ans;
80+
}
81+
};
82+
```
83+
84+
### **Go**
85+
86+
```go
87+
func xorOperation(n int, start int) (ans int) {
88+
for i := 0; i < n; i++ {
89+
ans ^= start + 2*i
90+
}
91+
return
92+
}
93+
```
94+
6995
### **...**
7096

7197
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int xorOperation(int n, int start) {
4+
int ans = 0;
5+
for (int i = 0; i < n; ++i) {
6+
ans ^= start + 2 * i;
7+
}
8+
return ans;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func xorOperation(n int, start int) (ans int) {
2+
for i := 0; i < n; i++ {
3+
ans ^= start + 2*i
4+
}
5+
return
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Solution {
22
public int xorOperation(int n, int start) {
3-
int ret = start;
4-
for (int i = 1; i < n; i++) {
5-
ret = ret ^ (start + (i << 1));
3+
int ans = 0;
4+
for (int i = 0; i < n; ++i) {
5+
ans ^= start + 2 * i;
66
}
7-
return ret;
7+
return ans;
88
}
99
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def xorOperation(self, n: int, start: int) -> int:
3-
res = 0
3+
ans = 0
44
for i in range(n):
5-
res ^= start + (i << 1)
6-
return res
5+
ans ^= start + 2 * i
6+
return ans

solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
**方法一:前缀和 + 计数器**
6565

66-
我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时$cnt[0] = 1$,而 $cnt[1] = 0$。
66+
我们定义一个长度为 $2$ 的数组 $cnt$ 作为计数器,其中 $cnt[0]$ 和 $cnt[1]$ 分别表示前缀和为偶数和奇数的子数组的个数。初始时 $cnt[0] = 1$,而 $cnt[1] = 0$。
6767

6868
接下来,我们维护当前的前缀和 $s$,初始时 $s = 0$。
6969

0 commit comments

Comments
 (0)