Skip to content

Commit b2ba7f6

Browse files
authored
feat: add solutions to lc problem: No.0961 (#4879)
1 parent d07a74c commit b2ba7f6

File tree

8 files changed

+271
-4
lines changed

8 files changed

+271
-4
lines changed

solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README.md

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ tags:
7373

7474
### 方法一:哈希表
7575

76-
由于数组 $nums$ 一共有 $2n$ 个元素,其中有 $n + 1$ 个不同的元素,且有一个元素重复了 $n$ 次,说明数组中的其余 $n$ 个元素都是不同的。
76+
由于数组 $\textit{nums}$ 一共有 $2n$ 个元素,其中有 $n + 1$ 个不同的元素,且有一个元素重复了 $n$ 次,说明数组中的其余 $n$ 个元素都是不同的。
7777

78-
因此,我们只需要遍历数组 $nums$,用哈希表 $s$ 记录遍历过的元素。当遍历到某个元素 $x$ 时,如果 $x$ 在哈希表 $s$ 中已经存在,说明 $x$ 是重复的元素,直接返回 $x$ 即可。
78+
因此,我们只需要遍历数组 $\textit{nums}$,用哈希表 $s$ 记录遍历过的元素。当遍历到某个元素 $x$ 时,如果 $x$ 在哈希表 $s$ 中已经存在,说明 $x$ 是重复的元素,直接返回 $x$ 即可。
7979

80-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
80+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8181

8282
<!-- tabs:start -->
8383

@@ -175,4 +175,107 @@ var repeatedNTimes = function (nums) {
175175

176176
<!-- solution:end -->
177177

178+
<!-- solution:start -->
179+
180+
### 方法二:数学
181+
182+
根据题目描述,数组 $\textit{nums}$ 中有一半的元素都是相同的,如果我们将数组视为环形排列,那么两个相同元素之间的最多间隔 $1$ 个其他元素。
183+
184+
因此,我们从下标 $2$ 开始遍历数组 $\textit{nums}$,对于每个下标 $i$,我们比较 $\textit{nums}[i]$ 与 $\textit{nums}[i - 1]$ 和 $\textit{nums}[i - 2]$ 的值,如果相等则返回该值。
185+
186+
如果没有在上述过程中找到重复的元素,那么重复的元素一定是 $\textit{nums}[0]$,我们直接返回 $\textit{nums}[0]$ 即可。
187+
188+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
189+
190+
<!-- tabs:start -->
191+
192+
#### Python3
193+
194+
```python
195+
class Solution:
196+
def repeatedNTimes(self, nums: List[int]) -> int:
197+
for i in range(2, len(nums)):
198+
if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]:
199+
return nums[i]
200+
return nums[0]
201+
```
202+
203+
#### Java
204+
205+
```java
206+
class Solution {
207+
public int repeatedNTimes(int[] nums) {
208+
for (int i = 2; i < nums.length; ++i) {
209+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
210+
return nums[i];
211+
}
212+
}
213+
return nums[0];
214+
}
215+
}
216+
```
217+
218+
#### C++
219+
220+
```cpp
221+
class Solution {
222+
public:
223+
int repeatedNTimes(vector<int>& nums) {
224+
for (int i = 2; i < nums.size(); ++i) {
225+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
226+
return nums[i];
227+
}
228+
}
229+
return nums[0];
230+
}
231+
};
232+
```
233+
234+
#### Go
235+
236+
```go
237+
func repeatedNTimes(nums []int) int {
238+
for i := 2; i < len(nums); i++ {
239+
if nums[i] == nums[i-1] || nums[i] == nums[i-2] {
240+
return nums[i]
241+
}
242+
}
243+
return nums[0]
244+
}
245+
```
246+
247+
#### TypeScript
248+
249+
```ts
250+
function repeatedNTimes(nums: number[]): number {
251+
for (let i = 2; i < nums.length; ++i) {
252+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
253+
return nums[i];
254+
}
255+
}
256+
return nums[0];
257+
}
258+
```
259+
260+
#### JavaScript
261+
262+
```js
263+
/**
264+
* @param {number[]} nums
265+
* @return {number}
266+
*/
267+
var repeatedNTimes = function (nums) {
268+
for (let i = 2; i < nums.length; ++i) {
269+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
270+
return nums[i];
271+
}
272+
}
273+
return nums[0];
274+
};
275+
```
276+
277+
<!-- tabs:end -->
278+
279+
<!-- solution:end -->
280+
178281
<!-- problem:end -->

solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README_EN.md

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ tags:
5454

5555
<!-- solution:start -->
5656

57-
### Solution 1
57+
### Solution 1: Hash Table
58+
59+
Since the array $\textit{nums}$ has a total of $2n$ elements, with $n + 1$ distinct elements, and one element repeated $n$ times, this means the remaining $n$ elements in the array are all distinct.
60+
61+
Therefore, we only need to iterate through the array $\textit{nums}$ and use a hash table $s$ to record the elements we've encountered. When we encounter an element $x$, if $x$ already exists in the hash table $s$, it means $x$ is the repeated element, and we can directly return $x$.
62+
63+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{nums}$.
5864

5965
<!-- tabs:start -->
6066

@@ -152,4 +158,107 @@ var repeatedNTimes = function (nums) {
152158

153159
<!-- solution:end -->
154160

161+
<!-- solution:start -->
162+
163+
### Solution 2: Mathematics
164+
165+
According to the problem description, half of the elements in the array $\textit{nums}$ are the same. If we view the array as a circular arrangement, then there is at most $1$ other element between two identical elements.
166+
167+
Therefore, we iterate through the array $\textit{nums}$ starting from index $2$. For each index $i$, we compare $\textit{nums}[i]$ with $\textit{nums}[i - 1]$ and $\textit{nums}[i - 2]$. If they are equal, we return that value.
168+
169+
If we don't find the repeated element in the above process, then the repeated element must be $\textit{nums}[0]$, and we can directly return $\textit{nums}[0]$.
170+
171+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
172+
173+
<!-- tabs:start -->
174+
175+
#### Python3
176+
177+
```python
178+
class Solution:
179+
def repeatedNTimes(self, nums: List[int]) -> int:
180+
for i in range(2, len(nums)):
181+
if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]:
182+
return nums[i]
183+
return nums[0]
184+
```
185+
186+
#### Java
187+
188+
```java
189+
class Solution {
190+
public int repeatedNTimes(int[] nums) {
191+
for (int i = 2; i < nums.length; ++i) {
192+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
193+
return nums[i];
194+
}
195+
}
196+
return nums[0];
197+
}
198+
}
199+
```
200+
201+
#### C++
202+
203+
```cpp
204+
class Solution {
205+
public:
206+
int repeatedNTimes(vector<int>& nums) {
207+
for (int i = 2; i < nums.size(); ++i) {
208+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
209+
return nums[i];
210+
}
211+
}
212+
return nums[0];
213+
}
214+
};
215+
```
216+
217+
#### Go
218+
219+
```go
220+
func repeatedNTimes(nums []int) int {
221+
for i := 2; i < len(nums); i++ {
222+
if nums[i] == nums[i-1] || nums[i] == nums[i-2] {
223+
return nums[i]
224+
}
225+
}
226+
return nums[0]
227+
}
228+
```
229+
230+
#### TypeScript
231+
232+
```ts
233+
function repeatedNTimes(nums: number[]): number {
234+
for (let i = 2; i < nums.length; ++i) {
235+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
236+
return nums[i];
237+
}
238+
}
239+
return nums[0];
240+
}
241+
```
242+
243+
#### JavaScript
244+
245+
```js
246+
/**
247+
* @param {number[]} nums
248+
* @return {number}
249+
*/
250+
var repeatedNTimes = function (nums) {
251+
for (let i = 2; i < nums.length; ++i) {
252+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
253+
return nums[i];
254+
}
255+
}
256+
return nums[0];
257+
};
258+
```
259+
260+
<!-- tabs:end -->
261+
262+
<!-- solution:end -->
263+
155264
<!-- problem:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int repeatedNTimes(vector<int>& nums) {
4+
for (int i = 2; i < nums.size(); ++i) {
5+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
6+
return nums[i];
7+
}
8+
}
9+
return nums[0];
10+
}
11+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func repeatedNTimes(nums []int) int {
2+
for i := 2; i < len(nums); i++ {
3+
if nums[i] == nums[i-1] || nums[i] == nums[i-2] {
4+
return nums[i]
5+
}
6+
}
7+
return nums[0]
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int repeatedNTimes(int[] nums) {
3+
for (int i = 2; i < nums.length; ++i) {
4+
if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) {
5+
return nums[i];
6+
}
7+
}
8+
return nums[0];
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var repeatedNTimes = function (nums) {
6+
for (let i = 2; i < nums.length; ++i) {
7+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
8+
return nums[i];
9+
}
10+
}
11+
return nums[0];
12+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def repeatedNTimes(self, nums: List[int]) -> int:
3+
for i in range(2, len(nums)):
4+
if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]:
5+
return nums[i]
6+
return nums[0]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function repeatedNTimes(nums: number[]): number {
2+
for (let i = 2; i < nums.length; ++i) {
3+
if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) {
4+
return nums[i];
5+
}
6+
}
7+
return nums[0];
8+
}

0 commit comments

Comments
 (0)