mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -109,6 +109,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n)
|
||||
* 空间复杂度: O(n)
|
||||
|
||||
## 总结
|
||||
|
||||
本题其实有四个重点:
|
||||
|
@ -83,6 +83,10 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n^2)
|
||||
* 空间复杂度: O(n),额外的 set 开销
|
||||
|
||||
|
||||
## 双指针
|
||||
|
||||
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
|
||||
@ -158,6 +162,10 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n^2)
|
||||
* 空间复杂度: O(1)
|
||||
|
||||
|
||||
## 去重逻辑的思考
|
||||
|
||||
### a的去重
|
||||
|
@ -121,6 +121,10 @@ public:
|
||||
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n^3)
|
||||
* 空间复杂度: O(1)
|
||||
|
||||
|
||||
## 补充
|
||||
|
||||
二级剪枝的部分:
|
||||
|
@ -178,16 +178,16 @@ var canJump = function(nums) {
|
||||
|
||||
```Rust
|
||||
impl Solution {
|
||||
fn max(a: usize, b: usize) -> usize {
|
||||
if a > b { a } else { b }
|
||||
}
|
||||
pub fn can_jump(nums: Vec<i32>) -> bool {
|
||||
let mut cover = 0;
|
||||
if (nums.len() == 1) { return true; }
|
||||
let mut i = 0;
|
||||
if nums.len() == 1 {
|
||||
return true;
|
||||
}
|
||||
let (mut i, mut cover) = (0, 0);
|
||||
while i <= cover {
|
||||
cover = Self::max(i + nums[i] as usize, cover);
|
||||
if cover >= nums.len() - 1 { return true; }
|
||||
cover = (i + nums[i] as usize).max(cover);
|
||||
if cover >= nums.len() - 1 {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
false
|
||||
|
@ -75,6 +75,8 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(logn)
|
||||
* 空间复杂度: O(logn)
|
||||
|
||||
|
||||
|
||||
|
@ -85,6 +85,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n)
|
||||
* 空间复杂度: O(1)
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
@ -72,6 +72,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(mn)
|
||||
* 空间复杂度: O(n)
|
||||
|
||||
## 拓展
|
||||
|
||||
那有同学可能问了,遇到哈希问题我直接都用set不就得了,用什么数组啊。
|
||||
@ -110,6 +113,8 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(m + n)
|
||||
* 空间复杂度: O(n)
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
@ -39,8 +39,6 @@ canConstruct("aa", "aab") -> true
|
||||
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
||||
|
||||
```CPP
|
||||
// 时间复杂度: O(n^2)
|
||||
// 空间复杂度:O(1)
|
||||
class Solution {
|
||||
public:
|
||||
bool canConstruct(string ransomNote, string magazine) {
|
||||
@ -62,6 +60,9 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n^2)
|
||||
* 空间复杂度: O(1)
|
||||
|
||||
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
||||
|
||||
|
||||
@ -78,8 +79,6 @@ public:
|
||||
代码如下:
|
||||
|
||||
```CPP
|
||||
// 时间复杂度: O(n)
|
||||
// 空间复杂度:O(1)
|
||||
class Solution {
|
||||
public:
|
||||
bool canConstruct(string ransomNote, string magazine) {
|
||||
@ -105,6 +104,10 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n)
|
||||
* 空间复杂度: O(1)
|
||||
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
@ -83,6 +83,9 @@ public:
|
||||
|
||||
```
|
||||
|
||||
* 时间复杂度: O(n^2)
|
||||
* 空间复杂度: O(n^2),最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user