Skip to content

Commit f815207

Browse files
committed
feat: update solutions to lc problem: No.0961. N-Repeated Element in Size 2N Array
1 parent fd4b14a commit f815207

File tree

6 files changed

+149
-42
lines changed

6 files changed

+149
-42
lines changed

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

+59-1
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,80 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
长度为 `2N`,共 `N+1` 个不同元素,其中一个元素出现 `N` 次,说明其它元素各不相同。
55+
56+
遍历数组,只要出现重复元素,它就是我们要找的重复 `N` 次的元素。
57+
5458
<!-- tabs:start -->
5559

5660
### **Python3**
5761

5862
<!-- 这里可写当前语言的特殊实现逻辑 -->
5963

6064
```python
61-
65+
class Solution:
66+
def repeatedNTimes(self, nums: List[int]) -> int:
67+
s = set()
68+
for num in nums:
69+
if num in s:
70+
return num
71+
s.add(num)
6272
```
6373

6474
### **Java**
6575

6676
<!-- 这里可写当前语言的特殊实现逻辑 -->
6777

6878
```java
79+
class Solution {
80+
public int repeatedNTimes(int[] nums) {
81+
Set<Integer> s = new HashSet<>();
82+
for (int num : nums) {
83+
if (s.contains(num)) {
84+
return num;
85+
}
86+
s.add(num);
87+
}
88+
return -1;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
int repeatedNTimes(vector<int>& nums) {
99+
unordered_set<int> s;
100+
for (auto &num : nums) {
101+
if (s.find(num) != s.end()) {
102+
return num;
103+
}
104+
s.insert(num);
105+
}
106+
return -1;
107+
}
108+
};
109+
```
69110
111+
### **JavaScript**
112+
113+
```js
114+
/**
115+
* @param {number[]} nums
116+
* @return {number}
117+
*/
118+
var repeatedNTimes = function(nums) {
119+
const s = new Set();
120+
for (const num of nums) {
121+
if (s.has(num)) {
122+
return num;
123+
}
124+
s.add(num);
125+
}
126+
return -1;
127+
};
70128
```
71129

72130
### **...**

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

+55-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,67 @@
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def repeatedNTimes(self, nums: List[int]) -> int:
63+
s = set()
64+
for num in nums:
65+
if num in s:
66+
return num
67+
s.add(num)
6268
```
6369

6470
### **Java**
6571

6672
```java
73+
class Solution {
74+
public int repeatedNTimes(int[] nums) {
75+
Set<Integer> s = new HashSet<>();
76+
for (int num : nums) {
77+
if (s.contains(num)) {
78+
return num;
79+
}
80+
s.add(num);
81+
}
82+
return -1;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int repeatedNTimes(vector<int>& nums) {
93+
unordered_set<int> s;
94+
for (auto &num : nums) {
95+
if (s.find(num) != s.end()) {
96+
return num;
97+
}
98+
s.insert(num);
99+
}
100+
return -1;
101+
}
102+
};
103+
```
67104
105+
### **JavaScript**
106+
107+
```js
108+
/**
109+
* @param {number[]} nums
110+
* @return {number}
111+
*/
112+
var repeatedNTimes = function(nums) {
113+
const s = new Set();
114+
for (const num of nums) {
115+
if (s.has(num)) {
116+
return num;
117+
}
118+
s.add(num);
119+
}
120+
return -1;
121+
};
68122
```
69123

70124
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
class Solution {
22
public:
3-
int repeatedNTimes(vector<int>& A) {
4-
unordered_set<int> us(A[0]) ;
5-
for(int i = (A.size() >> 1) - 1; i < A.size(); ++i)
6-
if(us.find(A[i]) != us.end())
7-
return A[i] ;
8-
else
9-
us.insert(A[i]) ;
10-
return A[0] ;
3+
int repeatedNTimes(vector<int>& nums) {
4+
unordered_set<int> s;
5+
for (auto &num : nums) {
6+
if (s.find(num) != s.end()) {
7+
return num;
8+
}
9+
s.insert(num);
10+
}
11+
return -1;
1112
}
1213
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
2-
public int repeatedNTimes(int[] A) {
3-
Set<Integer> set = new HashSet<>();
4-
for (int e : A) {
5-
if (set.contains(e)) {
6-
return e;
2+
public int repeatedNTimes(int[] nums) {
3+
Set<Integer> s = new HashSet<>();
4+
for (int num : nums) {
5+
if (s.contains(num)) {
6+
return num;
77
}
8-
set.add(e);
8+
s.add(num);
99
}
10-
return 0;
10+
return -1;
1111
}
1212
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
const repeatedNTimes = function (A) {
2-
let ss = new Set();
3-
for (let i = 0; i < A.length; i++) {
4-
if (ss.has(A[i])) {
5-
return A[i];
6-
}
7-
ss.add(A[i]);
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var repeatedNTimes = function(nums) {
6+
const s = new Set();
7+
for (const num of nums) {
8+
if (s.has(num)) {
9+
return num;
10+
}
11+
s.add(num);
812
}
9-
return null;
10-
};
13+
return -1;
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
class Solution:
2-
def repeatedNTimes(self, A):
3-
"""
4-
:type A: List[int]
5-
:rtype: int
6-
"""
7-
if A[0] == A[1] or A[0] == A[2] or A[0] == A[3]:
8-
return A[0]
9-
elif A[1] == A[2] or A[1] == A[3]:
10-
return A[1]
11-
elif A[2] == A[3]:
12-
return A[2]
13-
i = 4
14-
while i < len(A):
15-
if A[i] == A[i + 1]:
16-
return A[i]
17-
i += 2
2+
def repeatedNTimes(self, nums: List[int]) -> int:
3+
s = set()
4+
for num in nums:
5+
if num in s:
6+
return num
7+
s.add(num)

0 commit comments

Comments
 (0)