Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0217
Browse files Browse the repository at this point in the history
No.0217.Contains Duplicate
  • Loading branch information
yanglbme committed Nov 23, 2022
1 parent 54ec757 commit c7122a7
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 61 deletions.
14 changes: 5 additions & 9 deletions solution/0000-0099/0011.Container With Most Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,15 @@

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

双指针解决。
**方法一:双指针**

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

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

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

移动左侧的柱子中,重复进行上面的操作。

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

<!-- tabs:start -->

Expand Down
92 changes: 71 additions & 21 deletions solution/0200-0299/0217.Contains Duplicate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@

**方法一:排序**

排序数组,然后两个相邻元素是否相同即可。
我们先对数组 `nums` 进行排序。

然后遍历数组,如果存在相邻两个元素相同,说明数组中存在重复元素,直接返回 `true`

否则,遍历结束,返回 `false`

时间复杂度 $O(n \times \log n)$。其中 $n$ 是数组 `nums` 的长度。

**方法二:哈希表**

遍历元素并记录,当第二次出现时,直接返回 `true`
遍历数组,将出现过的元素记录在哈希表 $s$ 中。若元素第二次出现时,说明数组中存在重复元素,直接返回 `true`

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。

<!-- tabs:start -->

Expand All @@ -58,7 +66,13 @@
```python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
return any(a == b for a, b in pairwise(sorted(nums)))
```

```python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) < len(nums)
```

### **Java**
Expand All @@ -68,24 +82,28 @@ class Solution:
```java
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int num : nums) {
if (s.contains(num)) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
s.add(num);
}
return false;
}
}
```

### **TypeScript**

```ts
function containsDuplicate(nums: number[]): boolean {
let unique: Set<number> = new Set(nums);
return unique.size != nums.length;
```java
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int num : nums) {
if (!s.add(num)) {
return true;
}
}
return false;
}
}
```

Expand All @@ -95,31 +113,63 @@ function containsDuplicate(nums: number[]): boolean {
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int e : nums) {
if (s.count(e)) return true;
s.insert(e);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size() - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
};
```
```cpp
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s(nums.begin(), nums.end());
return s.size() < nums.size();
}
};
```

### **Go**

```go
func containsDuplicate(nums []int) bool {
s := make(map[int]bool)
for _, e := range nums {
if s[e] {
sort.Ints(nums)
for i, v := range nums[1:] {
if v == nums[i] {
return true
}
s[e] = true
}
return false
}
```

```go
func containsDuplicate(nums []int) bool {
s := map[int]bool{}
for _, v := range nums {
if s[v] {
return true
}
s[v] = true
}
return false
}
```

### **TypeScript**

```ts
function containsDuplicate(nums: number[]): boolean {
let unique: Set<number> = new Set(nums);
return unique.size != nums.length;
}
```

### **C#**

```cs
Expand Down
80 changes: 61 additions & 19 deletions solution/0200-0299/0217.Contains Duplicate/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,42 @@
```python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
return any(a == b for a, b in pairwise(sorted(nums)))
```

```python
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(set(nums)) < len(nums)
```

### **Java**

```java
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int num : nums) {
if (s.contains(num)) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
s.add(num);
}
return false;
}
}
```

### **TypeScript**

```ts
function containsDuplicate(nums: number[]): boolean {
let unique: Set<number> = new Set(nums);
return unique.size != nums.length;
```java
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int num : nums) {
if (!s.add(num)) {
return true;
}
}
return false;
}
}
```

Expand All @@ -69,31 +79,63 @@ function containsDuplicate(nums: number[]): boolean {
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int e : nums) {
if (s.count(e)) return true;
s.insert(e);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size() - 1; ++i) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
};
```
```cpp
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s(nums.begin(), nums.end());
return s.size() < nums.size();
}
};
```

### **Go**

```go
func containsDuplicate(nums []int) bool {
s := make(map[int]bool)
for _, e := range nums {
if s[e] {
sort.Ints(nums)
for i, v := range nums[1:] {
if v == nums[i] {
return true
}
s[e] = true
}
return false
}
```

```go
func containsDuplicate(nums []int) bool {
s := map[int]bool{}
for _, v := range nums {
if s[v] {
return true
}
s[v] = true
}
return false
}
```

### **TypeScript**

```ts
function containsDuplicate(nums: number[]): boolean {
let unique: Set<number> = new Set(nums);
return unique.size != nums.length;
}
```

### **C#**

```cs
Expand Down
8 changes: 2 additions & 6 deletions solution/0200-0299/0217.Contains Duplicate/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for (int e : nums) {
if (s.count(e)) return true;
s.insert(e);
}
return false;
unordered_set<int> s(nums.begin(), nums.end());
return s.size() < nums.size();
}
};
8 changes: 4 additions & 4 deletions solution/0200-0299/0217.Contains Duplicate/Solution.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
func containsDuplicate(nums []int) bool {
s := make(map[int]bool)
for _, e := range nums {
if s[e] {
s := map[int]bool{}
for _, v := range nums {
if s[v] {
return true
}
s[e] = true
s[v] = true
}
return false
}
3 changes: 1 addition & 2 deletions solution/0200-0299/0217.Contains Duplicate/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int num : nums) {
if (s.contains(num)) {
if (!s.add(num)) {
return true;
}
s.add(num);
}
return false;
}
Expand Down

0 comments on commit c7122a7

Please sign in to comment.