Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 566fd63

Browse files
feat: add solutions to lc problems: No.0961,0963 (doocs#1497)
* No.0961.N-Repeated Element in Size 2N Array * No.0963.Minimum Area Rectangle II
1 parent aaa7e7e commit 566fd63

File tree

15 files changed

+692
-79
lines changed

15 files changed

+692
-79
lines changed

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

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
长度为 `2N`,共 `N+1` 个不同元素,其中一个元素出现 `N` 次,说明其它元素各不相同。
61+
**方法一:哈希表**
6262

63-
遍历数组,只要出现重复元素,它就是我们要找的重复 `N` 次的元素。
63+
由于数组 $nums$ 一共有 2ドルn$ 个元素,其中有 $n + 1$ 个不同的元素,且有一个元素重复了 $n$ 次,说明数组中的其余 $n$ 个元素都是不同的。
64+
65+
因此,我们只需要遍历数组 $nums,ドル用哈希表 $s$ 记录遍历过的元素。当遍历到某个元素 $x$ 时,如果 $x$ 在哈希表 $s$ 中已经存在,说明 $x$ 是重复的元素,直接返回 $x$ 即可。
66+
67+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
6468

6569
<!-- tabs:start -->
6670

@@ -72,10 +76,10 @@
7276
class Solution:
7377
def repeatedNTimes(self, nums: List[int]) -> int:
7478
s = set()
75-
for num in nums:
76-
if num in s:
77-
return num
78-
s.add(num)
79+
for x in nums:
80+
if x in s:
81+
return x
82+
s.add(x)
7983
```
8084

8185
### **Java**
@@ -85,14 +89,12 @@ class Solution:
8589
```java
8690
class Solution {
8791
public int repeatedNTimes(int[] nums) {
88-
Set<Integer> s = new HashSet<>();
89-
for (int num : nums) {
90-
if (s.contains(num)) {
91-
return num;
92+
Set<Integer> s = new HashSet<>(nums.length /2+1);
93+
for (int i =0;; ++i) {
94+
if (!s.add(nums[i])) {
95+
return nums[i];
9296
}
93-
s.add(num);
9497
}
95-
return -1;
9698
}
9799
}
98100
```
@@ -104,17 +106,44 @@ class Solution {
104106
public:
105107
int repeatedNTimes(vector<int>& nums) {
106108
unordered_set<int> s;
107-
for (auto& num : nums) {
108-
if (s.find(num) != s.end()) {
109-
return num;
109+
for (int i = 0;; ++i) {
110+
if (s.count(nums[i])) {
111+
return nums[i];
110112
}
111-
s.insert(num);
113+
s.insert(nums[i]);
112114
}
113-
return -1;
114115
}
115116
};
116117
```
117118
119+
### **Go**
120+
121+
```go
122+
func repeatedNTimes(nums []int) int {
123+
s := map[int]bool{}
124+
for i := 0; ; i++ {
125+
if s[nums[i]] {
126+
return nums[i]
127+
}
128+
s[nums[i]] = true
129+
}
130+
}
131+
```
132+
133+
### **TypeScript**
134+
135+
```ts
136+
function repeatedNTimes(nums: number[]): number {
137+
const s: Set<number> = new Set();
138+
for (const x of nums) {
139+
if (s.has(x)) {
140+
return x;
141+
}
142+
s.add(x);
143+
}
144+
}
145+
```
146+
118147
### **JavaScript**
119148

120149
```js
@@ -124,13 +153,12 @@ public:
124153
*/
125154
var repeatedNTimes = function (nums) {
126155
const s = new Set();
127-
for (const num of nums) {
128-
if (s.has(num)) {
129-
return num;
156+
for (const x of nums) {
157+
if (s.has(x)) {
158+
return x;
130159
}
131-
s.add(num);
160+
s.add(x);
132161
}
133-
return -1;
134162
};
135163
```
136164

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,23 @@
4545
class Solution:
4646
def repeatedNTimes(self, nums: List[int]) -> int:
4747
s = set()
48-
for num in nums:
49-
if num in s:
50-
return num
51-
s.add(num)
48+
for x in nums:
49+
if x in s:
50+
return x
51+
s.add(x)
5252
```
5353

5454
### **Java**
5555

5656
```java
5757
class Solution {
5858
public int repeatedNTimes(int[] nums) {
59-
Set<Integer> s = new HashSet<>();
60-
for (int num : nums) {
61-
if (s.contains(num)) {
62-
return num;
59+
Set<Integer> s = new HashSet<>(nums.length /2+1);
60+
for (int i =0;; ++i) {
61+
if (!s.add(nums[i])) {
62+
return nums[i];
6363
}
64-
s.add(num);
6564
}
66-
return -1;
6765
}
6866
}
6967
```
@@ -75,17 +73,44 @@ class Solution {
7573
public:
7674
int repeatedNTimes(vector<int>& nums) {
7775
unordered_set<int> s;
78-
for (auto& num : nums) {
79-
if (s.find(num) != s.end()) {
80-
return num;
76+
for (int i = 0;; ++i) {
77+
if (s.count(nums[i])) {
78+
return nums[i];
8179
}
82-
s.insert(num);
80+
s.insert(nums[i]);
8381
}
84-
return -1;
8582
}
8683
};
8784
```
8885
86+
### **Go**
87+
88+
```go
89+
func repeatedNTimes(nums []int) int {
90+
s := map[int]bool{}
91+
for i := 0; ; i++ {
92+
if s[nums[i]] {
93+
return nums[i]
94+
}
95+
s[nums[i]] = true
96+
}
97+
}
98+
```
99+
100+
### **TypeScript**
101+
102+
```ts
103+
function repeatedNTimes(nums: number[]): number {
104+
const s: Set<number> = new Set();
105+
for (const x of nums) {
106+
if (s.has(x)) {
107+
return x;
108+
}
109+
s.add(x);
110+
}
111+
}
112+
```
113+
89114
### **JavaScript**
90115

91116
```js
@@ -95,13 +120,12 @@ public:
95120
*/
96121
var repeatedNTimes = function (nums) {
97122
const s = new Set();
98-
for (const num of nums) {
99-
if (s.has(num)) {
100-
return num;
123+
for (const x of nums) {
124+
if (s.has(x)) {
125+
return x;
101126
}
102-
s.add(num);
127+
s.add(x);
103128
}
104-
return -1;
105129
};
106130
```
107131

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
class Solution {
2-
public:
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;
12-
}
1+
class Solution {
2+
public:
3+
int repeatedNTimes(vector<int>& nums) {
4+
unordered_set<int> s;
5+
for (int i = 0;; ++i) {
6+
if (s.count(nums[i])) {
7+
return nums[i];
8+
}
9+
s.insert(nums[i]);
10+
}
11+
}
1312
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func repeatedNTimes(nums []int) int {
2+
s := map[int]bool{}
3+
for i := 0; ; i++ {
4+
if s[nums[i]] {
5+
return nums[i]
6+
}
7+
s[nums[i]] = true
8+
}
9+
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
class Solution {
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;
7-
}
8-
s.add(num);
9-
}
10-
return -1;
11-
}
1+
class Solution {
2+
public int repeatedNTimes(int[] nums) {
3+
Set<Integer> s = new HashSet<>(nums.length / 2 + 1);
4+
for (int i = 0;; ++i) {
5+
if (!s.add(nums[i])) {
6+
return nums[i];
7+
}
8+
}
9+
}
1210
}

‎solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
*/
55
var repeatedNTimes = function (nums) {
66
const s = new Set();
7-
for (const num of nums) {
8-
if (s.has(num)) {
9-
return num;
7+
for (const x of nums) {
8+
if (s.has(x)) {
9+
return x;
1010
}
11-
s.add(num);
11+
s.add(x);
1212
}
13-
return -1;
1413
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
class Solution:
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)
1+
class Solution:
2+
def repeatedNTimes(self, nums: List[int]) -> int:
3+
s = set()
4+
for x in nums:
5+
if x in s:
6+
return x
7+
s.add(x)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function repeatedNTimes(nums: number[]): number {
2+
const s: Set<number> = new Set();
3+
for (const x of nums) {
4+
if (s.has(x)) {
5+
return x;
6+
}
7+
s.add(x);
8+
}
9+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /