update: 0435.无重叠区间,rust版本代码报错

This commit is contained in:
WmW
2024-01-17 10:48:03 +08:00
parent 6016d2cf77
commit f0ac6f599c

View File

@ -4,7 +4,6 @@
</a>
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 435. 无重叠区间
[力扣题目链接](https://leetcode.cn/problems/non-overlapping-intervals/)
@ -16,19 +15,22 @@
区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。
示例 1:
* 输入: [ [1,2], [2,3], [3,4], [1,3] ]
*: 1
* 解释: 移除 [1,3] 后,剩下的区间没有重叠。
-: [ [1,2], [2,3], [3,4], [1,3] ]
- 输出: 1
- 解释: 移除 [1,3] 后,剩下的区间没有重叠。
示例 2:
* 输入: [ [1,2], [1,2], [1,2] ]
*: 2
* 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
-: [ [1,2], [1,2], [1,2] ]
- 输出: 2
- 解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
示例 3:
* 输入: [ [1,2], [2,3] ]
*: 0
* 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
-: [ [1,2], [2,3] ]
- 输出: 0
- 解释: 你不需要移除任何区间,因为它们已经是无重叠的了。
## 算法公开课
@ -84,8 +86,9 @@ public:
}
};
```
* 时间复杂度O(nlog n) ,有一个快排
* 间复杂度O(n),有一个快排,最差情况(倒序)时需要n次递归调用。因此确实需要O(n)的栈空间
- 间复杂度O(nlog n) ,有一个快排
- 空间复杂度O(n),有一个快排,最差情况(倒序)时,需要 n 次递归调用。因此确实需要 O(n)的栈空间
大家此时会发现如此复杂的一个问题,代码实现却这么简单!
@ -176,6 +179,7 @@ public:
```
这里按照 左边界排序,或者按照右边界排序,都可以 AC原理是一样的。
```CPP
class Solution {
public:
@ -204,8 +208,8 @@ public:
## 其他语言版本
### Java
```java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
@ -227,6 +231,7 @@ class Solution {
```
按左边排序,不管右边顺序。相交的时候取最小的右边。
```java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
@ -248,7 +253,9 @@ class Solution {
```
### Python
贪心 基于左边界
```python
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
@ -266,7 +273,9 @@ class Solution:
return count
```
贪心 基于左边界 把 452.用最少数量的箭引爆气球代码稍做修改
```python
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
@ -287,7 +296,9 @@ class Solution:
```
### Go
```go
func eraseOverlapIntervals(intervals [][]int) int {
sort.Slice(intervals, func(i, j int) bool {
@ -312,7 +323,9 @@ func min(a, b int) int {
```
### Javascript
- 按右边界排序
```Javascript
var eraseOverlapIntervals = function(intervals) {
intervals.sort((a, b) => {
@ -333,23 +346,25 @@ var eraseOverlapIntervals = function(intervals) {
return intervals.length - count
};
```
- 按左边界排序
```js
var eraseOverlapIntervals = function (intervals) {
// 按照左边界升序排列
intervals.sort((a, b) => a[0] - b[0])
let count = 1
let end = intervals[intervals.length - 1][0]
intervals.sort((a, b) => a[0] - b[0]);
let count = 1;
let end = intervals[intervals.length - 1][0];
// 倒序遍历,对单个区间来说,左边界越大越好,因为给前面区间的空间越大
for (let i = intervals.length - 2; i >= 0; i--) {
if (intervals[i][1] <= end) {
count++
end = intervals[i][0]
count++;
end = intervals[i][0];
}
}
// count 记录的是最大非重复区间的个数
return intervals.length - count
}
return intervals.length - count;
};
```
### TypeScript
@ -370,7 +385,7 @@ function eraseOverlapIntervals(intervals: number[][]): number {
}
}
return length - count;
};
}
```
> 按左边界排序,从左往右遍历
@ -394,7 +409,7 @@ function eraseOverlapIntervals(intervals: number[][]): number {
}
}
return resCount;
};
}
```
### Scala
@ -423,7 +438,7 @@ object Solution {
```Rust
impl Solution {
pub fn erase_overlap_intervals(intervals: Vec<Vec<i32>>) -> i32 {
pub fn erase_overlap_intervals(mut intervals: Vec<Vec<i32>>) -> i32 {
if intervals.is_empty() {
return 0;
}
@ -441,7 +456,9 @@ impl Solution {
}
}
```
### C#
```csharp
public class Solution
{
@ -463,9 +480,7 @@ public class Solution
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a>