Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Javie Deng
2023-03-22 16:07:06 +08:00
committed by GitHub
9 changed files with 43 additions and 12 deletions

View File

@ -109,6 +109,9 @@ public:
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
## 总结
本题其实有四个重点:

View File

@ -83,6 +83,10 @@ public:
};
```
* 时间复杂度: O(n^2)
* 空间复杂度: O(n),额外的 set 开销
## 双指针
**其实这道题目使用哈希法并不十分合适**因为在去重的操作中有很多细节需要注意在面试中很难直接写出没有bug的代码。
@ -158,6 +162,10 @@ public:
};
```
* 时间复杂度: O(n^2)
* 空间复杂度: O(1)
## 去重逻辑的思考
### a的去重

View File

@ -121,6 +121,10 @@ public:
```
* 时间复杂度: O(n^3)
* 空间复杂度: O(1)
## 补充
二级剪枝的部分:

View File

@ -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

View File

@ -75,6 +75,8 @@ public:
};
```
* 时间复杂度: O(logn)
* 空间复杂度: O(logn)

View File

@ -85,6 +85,9 @@ public:
};
```
* 时间复杂度: O(n)
* 空间复杂度: O(1)
## 其他语言版本

View File

@ -72,6 +72,9 @@ public:
};
```
* 时间复杂度: O(mn)
* 空间复杂度: O(n)
## 拓展
那有同学可能问了遇到哈希问题我直接都用set不就得了用什么数组啊。
@ -110,6 +113,8 @@ public:
};
```
* 时间复杂度: O(m + n)
* 空间复杂度: O(n)
## 其他语言版本

View File

@ -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)
## 其他语言版本

View File

@ -83,6 +83,9 @@ public:
```
* 时间复杂度: O(n^2)
* 空间复杂度: O(n^2)最坏情况下A和B的值各不相同相加产生的数字个数为 n^2