Skip to content

Commit a8def68

Browse files
authored
feat: add weekly contest 399 (doocs#2916)
1 parent 991db8f commit a8def68

File tree

34 files changed

+1486
-25
lines changed

34 files changed

+1486
-25
lines changed

solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Fi
7272

7373
<!-- solution:start -->
7474

75-
### 方法一
75+
### 方法一:计数
76+
77+
我们定义一个数组或哈希表 $\text{cnt}$ 记录每个数字出现的次数。
78+
79+
接下来,遍历数组 $\text{nums}$,当某个数字出现两次时,我们将其与答案进行异或运算。
80+
81+
最后返回答案即可。
82+
83+
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\text{nums}$ 的长度,而 $M$ 是数组 $\text{nums}$ 中的最大值或数组 $\text{nums}$ 不同数字的个数。
7684

7785
<!-- tabs:start -->
7886

7987
#### Python3
8088

8189
```python
82-
90+
class Solution:
91+
def duplicateNumbersXOR(self, nums: List[int]) -> int:
92+
cnt = Counter(nums)
93+
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
8394
```
8495

8596
#### Java
8697

8798
```java
88-
99+
class Solution {
100+
public int duplicateNumbersXOR(int[] nums) {
101+
int[] cnt = new int[51];
102+
int ans = 0;
103+
for (int x : nums) {
104+
if (++cnt[x] == 2) {
105+
ans ^= x;
106+
}
107+
}
108+
return ans;
109+
}
110+
}
89111
```
90112

91113
#### C++
92114

93115
```cpp
94-
116+
class Solution {
117+
public:
118+
int duplicateNumbersXOR(vector<int>& nums) {
119+
int cnt[51]{};
120+
int ans = 0;
121+
for (int x : nums) {
122+
if (++cnt[x] == 2) {
123+
ans ^= x;
124+
}
125+
}
126+
return ans;
127+
}
128+
};
95129
```
96130
97131
#### Go
98132
99133
```go
134+
func duplicateNumbersXOR(nums []int) (ans int) {
135+
cnt := [51]int{}
136+
for _, x := range nums {
137+
cnt[x]++
138+
if cnt[x] == 2 {
139+
ans ^= x
140+
}
141+
}
142+
return
143+
}
144+
```
100145

146+
#### TypeScript
147+
148+
```ts
149+
function duplicateNumbersXOR(nums: number[]): number {
150+
const cnt: number[] = Array(51).fill(0);
151+
let ans: number = 0;
152+
for (const x of nums) {
153+
if (++cnt[x] === 2) {
154+
ans ^= x;
155+
}
156+
}
157+
return ans;
158+
}
101159
```
102160

103161
<!-- tabs:end -->

solution/3100-3199/3158.Find the XOR of Numbers Which Appear Twice/README_EN.md

+62-4
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Fi
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Counting
74+
75+
We define an array or hash table `cnt` to record the occurrence of each number.
76+
77+
Next, we traverse the array `nums`. When a number appears twice, we perform an XOR operation with the answer.
78+
79+
Finally, we return the answer.
80+
81+
The time complexity is $O(n)$, and the space complexity is $O(M)$. Where $n$ is the length of the array `nums`, and $M$ is the maximum value in the array `nums` or the number of distinct numbers in the array `nums`.
7482

7583
<!-- tabs:start -->
7684

7785
#### Python3
7886

7987
```python
80-
88+
class Solution:
89+
def duplicateNumbersXOR(self, nums: List[int]) -> int:
90+
cnt = Counter(nums)
91+
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
8192
```
8293

8394
#### Java
8495

8596
```java
86-
97+
class Solution {
98+
public int duplicateNumbersXOR(int[] nums) {
99+
int[] cnt = new int[51];
100+
int ans = 0;
101+
for (int x : nums) {
102+
if (++cnt[x] == 2) {
103+
ans ^= x;
104+
}
105+
}
106+
return ans;
107+
}
108+
}
87109
```
88110

89111
#### C++
90112

91113
```cpp
92-
114+
class Solution {
115+
public:
116+
int duplicateNumbersXOR(vector<int>& nums) {
117+
int cnt[51]{};
118+
int ans = 0;
119+
for (int x : nums) {
120+
if (++cnt[x] == 2) {
121+
ans ^= x;
122+
}
123+
}
124+
return ans;
125+
}
126+
};
93127
```
94128
95129
#### Go
96130
97131
```go
132+
func duplicateNumbersXOR(nums []int) (ans int) {
133+
cnt := [51]int{}
134+
for _, x := range nums {
135+
cnt[x]++
136+
if cnt[x] == 2 {
137+
ans ^= x
138+
}
139+
}
140+
return
141+
}
142+
```
98143

144+
#### TypeScript
145+
146+
```ts
147+
function duplicateNumbersXOR(nums: number[]): number {
148+
const cnt: number[] = Array(51).fill(0);
149+
let ans: number = 0;
150+
for (const x of nums) {
151+
if (++cnt[x] === 2) {
152+
ans ^= x;
153+
}
154+
}
155+
return ans;
156+
}
99157
```
100158

101159
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int duplicateNumbersXOR(vector<int>& nums) {
4+
int cnt[51]{};
5+
int ans = 0;
6+
for (int x : nums) {
7+
if (++cnt[x] == 2) {
8+
ans ^= x;
9+
}
10+
}
11+
return ans;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func duplicateNumbersXOR(nums []int) (ans int) {
2+
cnt := [51]int{}
3+
for _, x := range nums {
4+
cnt[x]++
5+
if cnt[x] == 2 {
6+
ans ^= x
7+
}
8+
}
9+
return
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int duplicateNumbersXOR(int[] nums) {
3+
int[] cnt = new int[51];
4+
int ans = 0;
5+
for (int x : nums) {
6+
if (++cnt[x] == 2) {
7+
ans ^= x;
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def duplicateNumbersXOR(self, nums: List[int]) -> int:
3+
cnt = Counter(nums)
4+
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function duplicateNumbersXOR(nums: number[]): number {
2+
const cnt: number[] = Array(51).fill(0);
3+
let ans: number = 0;
4+
for (const x of nums) {
5+
if (++cnt[x] === 2) {
6+
ans ^= x;
7+
}
8+
}
9+
return ans;
10+
}

solution/3100-3199/3159.Find Occurrences of an Element in an Array/README.md

+70-4
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,98 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Fi
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:模拟
73+
74+
根据题目描述,我们可以先遍历一遍数组 $\text{nums}$,找出所有值为 $x$ 的元素的下标,记录在数组 $\text{ids}$ 中。
75+
76+
接着遍历数组 $\text{queries}$,对于每个查询 $i$,如果 $i - 1$ 小于 $\text{ids}$ 的长度,那么答案就是 $\text{ids}[i - 1]$,否则答案就是 $-1$。
77+
78+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是数组 $\text{nums}$ 和数组 $\text{queries}$ 的长度。
7379

7480
<!-- tabs:start -->
7581

7682
#### Python3
7783

7884
```python
79-
85+
class Solution:
86+
def occurrencesOfElement(
87+
self, nums: List[int], queries: List[int], x: int
88+
) -> List[int]:
89+
ids = [i for i, v in enumerate(nums) if v == x]
90+
return [ids[i - 1] if i - 1 < len(ids) else -1 for i in queries]
8091
```
8192

8293
#### Java
8394

8495
```java
85-
96+
class Solution {
97+
public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
98+
List<Integer> ids = new ArrayList<>();
99+
for (int i = 0; i < nums.length; ++i) {
100+
if (nums[i] == x) {
101+
ids.add(i);
102+
}
103+
}
104+
int m = queries.length;
105+
int[] ans = new int[m];
106+
for (int i = 0; i < m; ++i) {
107+
int j = queries[i] - 1;
108+
ans[i] = j < ids.size() ? ids.get(j) : -1;
109+
}
110+
return ans;
111+
}
112+
}
86113
```
87114

88115
#### C++
89116

90117
```cpp
91-
118+
class Solution {
119+
public:
120+
vector<int> occurrencesOfElement(vector<int>& nums, vector<int>& queries, int x) {
121+
vector<int> ids;
122+
for (int i = 0; i < nums.size(); ++i) {
123+
if (nums[i] == x) {
124+
ids.push_back(i);
125+
}
126+
}
127+
vector<int> ans;
128+
for (int& i : queries) {
129+
ans.push_back(i - 1 < ids.size() ? ids[i - 1] : -1);
130+
}
131+
return ans;
132+
}
133+
};
92134
```
93135
94136
#### Go
95137
96138
```go
139+
func occurrencesOfElement(nums []int, queries []int, x int) (ans []int) {
140+
ids := []int{}
141+
for i, v := range nums {
142+
if v == x {
143+
ids = append(ids, i)
144+
}
145+
}
146+
for _, i := range queries {
147+
if i-1 < len(ids) {
148+
ans = append(ans, ids[i-1])
149+
} else {
150+
ans = append(ans, -1)
151+
}
152+
}
153+
return
154+
}
155+
```
156+
157+
#### TypeScript
97158

159+
```ts
160+
function occurrencesOfElement(nums: number[], queries: number[], x: number): number[] {
161+
const ids: number[] = nums.map((v, i) => (v === x ? i : -1)).filter(v => v !== -1);
162+
return queries.map(i => ids[i - 1] ?? -1);
163+
}
98164
```
99165

100166
<!-- tabs:end -->

0 commit comments

Comments
 (0)