Skip to content

feat: add biweekly contest 131 #2916

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 1 commit into from
May 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Fi

<!-- solution:start -->

### 方法一
### 方法一:计数

我们定义一个数组或哈希表 $\text{cnt}$ 记录每个数字出现的次数。

接下来,遍历数组 $\text{nums}$,当某个数字出现两次时,我们将其与答案进行异或运算。

最后返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是数组 $\text{nums}$ 的长度,而 $M$ 是数组 $\text{nums}$ 中的最大值或数组 $\text{nums}$ 不同数字的个数。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def duplicateNumbersXOR(self, nums: List[int]) -> int:
cnt = Counter(nums)
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
```

#### Java

```java

class Solution {
public int duplicateNumbersXOR(int[] nums) {
int[] cnt = new int[51];
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int duplicateNumbersXOR(vector<int>& nums) {
int cnt[51]{};
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
};
```

#### Go

```go
func duplicateNumbersXOR(nums []int) (ans int) {
cnt := [51]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] == 2 {
ans ^= x
}
}
return
}
```

#### TypeScript

```ts
function duplicateNumbersXOR(nums: number[]): number {
const cnt: number[] = Array(51).fill(0);
let ans: number = 0;
for (const x of nums) {
if (++cnt[x] === 2) {
ans ^= x;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3158.Fi

<!-- solution:start -->

### Solution 1
### Solution 1: Counting

We define an array or hash table `cnt` to record the occurrence of each number.

Next, we traverse the array `nums`. When a number appears twice, we perform an XOR operation with the answer.

Finally, we return the answer.

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`.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def duplicateNumbersXOR(self, nums: List[int]) -> int:
cnt = Counter(nums)
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
```

#### Java

```java

class Solution {
public int duplicateNumbersXOR(int[] nums) {
int[] cnt = new int[51];
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int duplicateNumbersXOR(vector<int>& nums) {
int cnt[51]{};
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
};
```

#### Go

```go
func duplicateNumbersXOR(nums []int) (ans int) {
cnt := [51]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] == 2 {
ans ^= x
}
}
return
}
```

#### TypeScript

```ts
function duplicateNumbersXOR(nums: number[]): number {
const cnt: number[] = Array(51).fill(0);
let ans: number = 0;
for (const x of nums) {
if (++cnt[x] === 2) {
ans ^= x;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int duplicateNumbersXOR(vector<int>& nums) {
int cnt[51]{};
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func duplicateNumbersXOR(nums []int) (ans int) {
cnt := [51]int{}
for _, x := range nums {
cnt[x]++
if cnt[x] == 2 {
ans ^= x
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int duplicateNumbersXOR(int[] nums) {
int[] cnt = new int[51];
int ans = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans ^= x;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def duplicateNumbersXOR(self, nums: List[int]) -> int:
cnt = Counter(nums)
return reduce(xor, [x for x, v in cnt.items() if v == 2], 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function duplicateNumbersXOR(nums: number[]): number {
const cnt: number[] = Array(51).fill(0);
let ans: number = 0;
for (const x of nums) {
if (++cnt[x] === 2) {
ans ^= x;
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,98 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3159.Fi

<!-- solution:start -->

### 方法一
### 方法一:模拟

根据题目描述,我们可以先遍历一遍数组 $\text{nums}$,找出所有值为 $x$ 的元素的下标,记录在数组 $\text{ids}$ 中。

接着遍历数组 $\text{queries}$,对于每个查询 $i$,如果 $i - 1$ 小于 $\text{ids}$ 的长度,那么答案就是 $\text{ids}[i - 1]$,否则答案就是 $-1$。

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

<!-- tabs:start -->

#### Python3

```python

class Solution:
def occurrencesOfElement(
self, nums: List[int], queries: List[int], x: int
) -> List[int]:
ids = [i for i, v in enumerate(nums) if v == x]
return [ids[i - 1] if i - 1 < len(ids) else -1 for i in queries]
```

#### Java

```java

class Solution {
public int[] occurrencesOfElement(int[] nums, int[] queries, int x) {
List<Integer> ids = new ArrayList<>();
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == x) {
ids.add(i);
}
}
int m = queries.length;
int[] ans = new int[m];
for (int i = 0; i < m; ++i) {
int j = queries[i] - 1;
ans[i] = j < ids.size() ? ids.get(j) : -1;
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<int> occurrencesOfElement(vector<int>& nums, vector<int>& queries, int x) {
vector<int> ids;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == x) {
ids.push_back(i);
}
}
vector<int> ans;
for (int& i : queries) {
ans.push_back(i - 1 < ids.size() ? ids[i - 1] : -1);
}
return ans;
}
};
```

#### Go

```go
func occurrencesOfElement(nums []int, queries []int, x int) (ans []int) {
ids := []int{}
for i, v := range nums {
if v == x {
ids = append(ids, i)
}
}
for _, i := range queries {
if i-1 < len(ids) {
ans = append(ans, ids[i-1])
} else {
ans = append(ans, -1)
}
}
return
}
```

#### TypeScript

```ts
function occurrencesOfElement(nums: number[], queries: number[], x: number): number[] {
const ids: number[] = nums.map((v, i) => (v === x ? i : -1)).filter(v => v !== -1);
return queries.map(i => ids[i - 1] ?? -1);
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading