Skip to content

Commit c7122a7

Browse files
committed
feat: update solutions to lc problem: No.0217
No.0217.Contains Duplicate
1 parent 54ec757 commit c7122a7

File tree

6 files changed

+144
-61
lines changed

6 files changed

+144
-61
lines changed

solution/0000-0099/0011.Container With Most Water/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,15 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
双指针解决。
49+
**方法一:双指针**
5050

51-
一开始,我们考虑相距最远的两个柱子所能容纳水的面积。水的宽度是两根柱子之间的距离,而水的高度取决于两根柱子之间较短的那个。
51+
一开始,我们考虑相距最远的两个柱子所能容纳水的容量。水的宽度是两根柱子之间的距离,而水的高度取决于两根柱子之间较短的那个。
5252

53-
- 当前柱子是最两侧的柱子,水的宽度最大,其他的组合,水的宽度都比这个小;
54-
- 当前左侧柱子较短,决定了水的高度。如果移动左侧的柱子,新的水面高度不确定,但一定不会超过右侧的柱子高度;
55-
- 如果移动右侧的柱子,新的水面高度一定不会超过左侧的柱子高度,也就是不会超过当前的水面高度。
53+
当前柱子是最两侧的柱子,水的宽度最大,其他的组合,水的宽度都比这个小。不妨假设左侧柱子的高度小于等于右侧柱子的高度,那么水的高度就是左侧柱子的高度。如果我们移动右侧柱子,那么水的宽度就减小了,而水的高度却不会增加,因此水的容量一定减少。所以我们移动左侧柱子,更新最大容量。
5654

57-
可见,如果固定左侧的柱子,向内移动右侧的柱子,水的高度一定不会增加,且宽度一定减少,所以水的面积一定减少。所以左侧的柱子跟右侧其他柱子的组合,都可以排除了。也就是代码中的 `i++`
55+
循环此过程,直到两个柱子相遇
5856

59-
移动左侧的柱子中,重复进行上面的操作。
60-
61-
在此过程中,我们不断排除掉无法成为构成最大值的柱子组合,而每一次都获取到可能为最大值的面积 t。那么遍历结束之后,我们就可以得到最大值。
57+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `height` 的长度。
6258

6359
<!-- tabs:start -->
6460

solution/0200-0299/0217.Contains Duplicate/README.md

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@
4343

4444
**方法一:排序**
4545

46-
排序数组,然后两个相邻元素是否相同即可。
46+
我们先对数组 `nums` 进行排序。
47+
48+
然后遍历数组,如果存在相邻两个元素相同,说明数组中存在重复元素,直接返回 `true`
49+
50+
否则,遍历结束,返回 `false`
51+
52+
时间复杂度 $O(n \times \log n)$。其中 $n$ 是数组 `nums` 的长度。
4753

4854
**方法二:哈希表**
4955

50-
遍历元素并记录,当第二次出现时,直接返回 `true`
56+
遍历数组,将出现过的元素记录在哈希表 $s$ 中。若元素第二次出现时,说明数组中存在重复元素,直接返回 `true`
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
5159

5260
<!-- tabs:start -->
5361

@@ -58,7 +66,13 @@
5866
```python
5967
class Solution:
6068
def containsDuplicate(self, nums: List[int]) -> bool:
61-
return len(nums) != len(set(nums))
69+
return any(a == b for a, b in pairwise(sorted(nums)))
70+
```
71+
72+
```python
73+
class Solution:
74+
def containsDuplicate(self, nums: List[int]) -> bool:
75+
return len(set(nums)) < len(nums)
6276
```
6377

6478
### **Java**
@@ -68,24 +82,28 @@ class Solution:
6882
```java
6983
class Solution {
7084
public boolean containsDuplicate(int[] nums) {
71-
Set<Integer> s = new HashSet<>();
72-
for (int num : nums) {
73-
if (s.contains(num)) {
85+
Arrays.sort(nums);
86+
for (int i = 0; i < nums.length - 1; ++i) {
87+
if (nums[i] == nums[i + 1]) {
7488
return true;
7589
}
76-
s.add(num);
7790
}
7891
return false;
7992
}
8093
}
8194
```
8295

83-
### **TypeScript**
84-
85-
```ts
86-
function containsDuplicate(nums: number[]): boolean {
87-
let unique: Set<number> = new Set(nums);
88-
return unique.size != nums.length;
96+
```java
97+
class Solution {
98+
public boolean containsDuplicate(int[] nums) {
99+
Set<Integer> s = new HashSet<>();
100+
for (int num : nums) {
101+
if (!s.add(num)) {
102+
return true;
103+
}
104+
}
105+
return false;
106+
}
89107
}
90108
```
91109

@@ -95,31 +113,63 @@ function containsDuplicate(nums: number[]): boolean {
95113
class Solution {
96114
public:
97115
bool containsDuplicate(vector<int>& nums) {
98-
unordered_set<int> s;
99-
for (int e : nums) {
100-
if (s.count(e)) return true;
101-
s.insert(e);
116+
sort(nums.begin(), nums.end());
117+
for (int i = 0; i < nums.size() - 1; ++i) {
118+
if (nums[i] == nums[i + 1]) {
119+
return true;
120+
}
102121
}
103122
return false;
104123
}
105124
};
106125
```
107126
127+
```cpp
128+
class Solution {
129+
public:
130+
bool containsDuplicate(vector<int>& nums) {
131+
unordered_set<int> s(nums.begin(), nums.end());
132+
return s.size() < nums.size();
133+
}
134+
};
135+
```
136+
108137
### **Go**
109138

110139
```go
111140
func containsDuplicate(nums []int) bool {
112-
s := make(map[int]bool)
113-
for _, e := range nums {
114-
if s[e] {
141+
sort.Ints(nums)
142+
for i, v := range nums[1:] {
143+
if v == nums[i] {
115144
return true
116145
}
117-
s[e] = true
118146
}
119147
return false
120148
}
121149
```
122150

151+
```go
152+
func containsDuplicate(nums []int) bool {
153+
s := map[int]bool{}
154+
for _, v := range nums {
155+
if s[v] {
156+
return true
157+
}
158+
s[v] = true
159+
}
160+
return false
161+
}
162+
```
163+
164+
### **TypeScript**
165+
166+
```ts
167+
function containsDuplicate(nums: number[]): boolean {
168+
let unique: Set<number> = new Set(nums);
169+
return unique.size != nums.length;
170+
}
171+
```
172+
123173
### **C#**
124174

125175
```cs

solution/0200-0299/0217.Contains Duplicate/README_EN.md

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,42 @@
3434
```python
3535
class Solution:
3636
def containsDuplicate(self, nums: List[int]) -> bool:
37-
return len(nums) != len(set(nums))
37+
return any(a == b for a, b in pairwise(sorted(nums)))
38+
```
39+
40+
```python
41+
class Solution:
42+
def containsDuplicate(self, nums: List[int]) -> bool:
43+
return len(set(nums)) < len(nums)
3844
```
3945

4046
### **Java**
4147

4248
```java
4349
class Solution {
4450
public boolean containsDuplicate(int[] nums) {
45-
Set<Integer> s = new HashSet<>();
46-
for (int num : nums) {
47-
if (s.contains(num)) {
51+
Arrays.sort(nums);
52+
for (int i = 0; i < nums.length - 1; ++i) {
53+
if (nums[i] == nums[i + 1]) {
4854
return true;
4955
}
50-
s.add(num);
5156
}
5257
return false;
5358
}
5459
}
5560
```
5661

57-
### **TypeScript**
58-
59-
```ts
60-
function containsDuplicate(nums: number[]): boolean {
61-
let unique: Set<number> = new Set(nums);
62-
return unique.size != nums.length;
62+
```java
63+
class Solution {
64+
public boolean containsDuplicate(int[] nums) {
65+
Set<Integer> s = new HashSet<>();
66+
for (int num : nums) {
67+
if (!s.add(num)) {
68+
return true;
69+
}
70+
}
71+
return false;
72+
}
6373
}
6474
```
6575

@@ -69,31 +79,63 @@ function containsDuplicate(nums: number[]): boolean {
6979
class Solution {
7080
public:
7181
bool containsDuplicate(vector<int>& nums) {
72-
unordered_set<int> s;
73-
for (int e : nums) {
74-
if (s.count(e)) return true;
75-
s.insert(e);
82+
sort(nums.begin(), nums.end());
83+
for (int i = 0; i < nums.size() - 1; ++i) {
84+
if (nums[i] == nums[i + 1]) {
85+
return true;
86+
}
7687
}
7788
return false;
7889
}
7990
};
8091
```
8192
93+
```cpp
94+
class Solution {
95+
public:
96+
bool containsDuplicate(vector<int>& nums) {
97+
unordered_set<int> s(nums.begin(), nums.end());
98+
return s.size() < nums.size();
99+
}
100+
};
101+
```
102+
82103
### **Go**
83104

84105
```go
85106
func containsDuplicate(nums []int) bool {
86-
s := make(map[int]bool)
87-
for _, e := range nums {
88-
if s[e] {
107+
sort.Ints(nums)
108+
for i, v := range nums[1:] {
109+
if v == nums[i] {
89110
return true
90111
}
91-
s[e] = true
92112
}
93113
return false
94114
}
95115
```
96116

117+
```go
118+
func containsDuplicate(nums []int) bool {
119+
s := map[int]bool{}
120+
for _, v := range nums {
121+
if s[v] {
122+
return true
123+
}
124+
s[v] = true
125+
}
126+
return false
127+
}
128+
```
129+
130+
### **TypeScript**
131+
132+
```ts
133+
function containsDuplicate(nums: number[]): boolean {
134+
let unique: Set<number> = new Set(nums);
135+
return unique.size != nums.length;
136+
}
137+
```
138+
97139
### **C#**
98140

99141
```cs
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
class Solution {
22
public:
33
bool containsDuplicate(vector<int>& nums) {
4-
unordered_set<int> s;
5-
for (int e : nums) {
6-
if (s.count(e)) return true;
7-
s.insert(e);
8-
}
9-
return false;
4+
unordered_set<int> s(nums.begin(), nums.end());
5+
return s.size() < nums.size();
106
}
117
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
func containsDuplicate(nums []int) bool {
2-
s := make(map[int]bool)
3-
for _, e := range nums {
4-
if s[e] {
2+
s := map[int]bool{}
3+
for _, v := range nums {
4+
if s[v] {
55
return true
66
}
7-
s[e] = true
7+
s[v] = true
88
}
99
return false
1010
}

solution/0200-0299/0217.Contains Duplicate/Solution.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ class Solution {
22
public boolean containsDuplicate(int[] nums) {
33
Set<Integer> s = new HashSet<>();
44
for (int num : nums) {
5-
if (s.contains(num)) {
5+
if (!s.add(num)) {
66
return true;
77
}
8-
s.add(num);
98
}
109
return false;
1110
}

0 commit comments

Comments
 (0)