Merge branch 'youngyangyang04:master' into master

This commit is contained in:
Jeremy Feng
2023-04-09 23:26:54 +08:00
committed by GitHub
14 changed files with 135 additions and 13 deletions

View File

@ -216,6 +216,26 @@ impl Solution {
} }
} }
``` ```
Rust
```
use std::collections::HashMap;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut hm: HashMap<i32, i32> = HashMap::new();
for i in 0..nums.len() {
let j = target - nums[i];
if hm.contains_key(&j) {
return vec![*hm.get(&j).unwrap(), i as i32]
} else {
hm.insert(nums[i], i as i32);
}
}
vec![-1, -1]
}
}
```
Javascript Javascript

View File

@ -135,6 +135,9 @@ public:
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。 技巧性的东西没有固定的学习方法,还是要多看多练,自己灵活运用了。

View File

@ -177,6 +177,34 @@ func max(a, b int) int {
return b return b
} }
``` ```
```go
// 版本2
func merge(intervals [][]int) [][]int {
if len(intervals) == 1 {
return intervals
}
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
res := make([][]int, 0)
res = append(res, intervals[0])
for i := 1; i < len(intervals); i++ {
if intervals[i][0] <= res[len(res)-1][1]{
res[len(res)-1][1] = max56(res[len(res)-1][1],intervals[i][1])
} else {
res = append(res, intervals[i])
}
}
return res
}
func max56(a, b int) int {
if a > b {
return a
}
return b
}
```
### Javascript ### Javascript
```javascript ```javascript

View File

@ -72,7 +72,7 @@ dp[i] 爬到第i层楼梯有dp[i]种方法
3. dp数组如何初始化 3. dp数组如何初始化
再回顾一下dp[i]的定义爬到第i层楼梯有dp[i]方法。 再回顾一下dp[i]的定义爬到第i层楼梯有dp[i]方法。
那么i为0dp[i]应该是多少呢,这个可以有很多解释,但基本都是直接奔着答案去解释的。 那么i为0dp[i]应该是多少呢,这个可以有很多解释,但基本都是直接奔着答案去解释的。

View File

@ -113,6 +113,9 @@ public:
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
## 题外话 ## 题外话

View File

@ -111,6 +111,8 @@ public:
} }
}; };
``` ```
* 时间复杂度: push为O(n)其他为O(1)
* 空间复杂度: O(n)
# 优化 # 优化
@ -156,6 +158,9 @@ public:
} }
}; };
``` ```
* 时间复杂度: push为O(n)其他为O(1)
* 空间复杂度: O(n)
# 其他语言版本 # 其他语言版本

View File

@ -112,6 +112,10 @@ public:
``` ```
* 时间复杂度: push和empty为O(1), pop和peek为O(n)
* 空间复杂度: O(n)
## 拓展 ## 拓展
可以看出peek()的实现直接复用了pop() 要不然对stOut判空的逻辑又要重写一遍。 可以看出peek()的实现直接复用了pop() 要不然对stOut判空的逻辑又要重写一遍。

View File

@ -184,6 +184,9 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(k)
再来看一下时间复杂度,使用单调队列的时间复杂度是 O(n)。 再来看一下时间复杂度,使用单调队列的时间复杂度是 O(n)。

View File

@ -79,8 +79,6 @@
```CPP ```CPP
// 时间复杂度O(nlogk)
// 空间复杂度O(n)
class Solution { class Solution {
public: public:
// 小顶堆 // 小顶堆
@ -120,6 +118,10 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(nlogk)
* 空间复杂度: O(n)
# 拓展 # 拓展
大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。 大家对这个比较运算在建堆时是如何应用的,为什么左大于右就会建立小顶堆,反而建立大顶堆比较困惑。

View File

@ -97,26 +97,25 @@ Java
```Java ```Java
class Solution { class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int temp;
int res = 0; int res = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//统计两个数组中的元素之和同时统计出现的次数放入map //统计两个数组中的元素之和同时统计出现的次数放入map
for (int i : nums1) { for (int i : nums1) {
for (int j : nums2) { for (int j : nums2) {
temp = i + j; int tmp = map.getOrDefault(i + j, 0);
if (map.containsKey(temp)) { if (tmp == 0) {
map.put(temp, map.get(temp) + 1); map.put(i + j, 1);
} else { } else {
map.put(temp, 1); map.replace(i + j, tmp + 1);
} }
} }
} }
//统计剩余的两个元素的和在map中找是否存在相加为0的情况同时记录次数 //统计剩余的两个元素的和在map中找是否存在相加为0的情况同时记录次数
for (int i : nums3) { for (int i : nums3) {
for (int j : nums4) { for (int j : nums4) {
temp = i + j; int tmp = map.getOrDefault(0 - i - j, 0);
if (map.containsKey(0 - temp)) { if (tmp != 0) {
res += map.get(0 - temp); res += tmp;
} }
} }
} }

View File

@ -215,6 +215,27 @@ class Solution {
} }
} }
``` ```
```Java
// 二维dp数组版本方便理解
class Solution {
public int change(int amount, int[] coins) {
int[][] dp = new int[coins.length][amount + 1];
// 只有一种硬币的情况
for (int i = 0; i <= amount; i += coins[0]) {
dp[0][i] = 1;
}
for (int i = 1; i < coins.length; i++) {
for (int j = 0; j <= amount; j++) {
// 第i种硬币使用0~k次求和
for (int k = 0; k * coins[i] <= j; k++) {
dp[i][j] += dp[i - 1][j - k * coins[i]];
}
}
}
return dp[coins.length - 1][amount];
}
}
```
Python Python

View File

@ -92,7 +92,7 @@ dp[i - 2] 跳到 dp[i] 需要花费 dp[i - 2] + cost[i - 2]。
这里就要说明本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。 这里就要说明本题力扣为什么改题意,而且修改题意之后 就清晰很多的原因了。
新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。 新题目描述中明确说了 “你可以选择从下标为 0 或下标为 1 的台阶开始爬楼梯。” 也就是说 到达 第 0 个台阶是不花费的,但从 第0 个台阶 往上跳的话,需要花费 cost[0]。
所以初始化 dp[0] = 0dp[1] = 0; 所以初始化 dp[0] = 0dp[1] = 0;

View File

@ -77,6 +77,9 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(n)
当然可以拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。 当然可以拿字符串直接作为栈,这样省去了栈还要转为字符串的操作。
@ -99,6 +102,8 @@ public:
} }
}; };
``` ```
* 时间复杂度: O(n)
* 空间复杂度: O(1),返回值不计空间复杂度
## 题外话 ## 题外话

View File

@ -177,6 +177,35 @@ Java
Python Python
Rust
```rust
// 版本二使用list链表
use std::collections::LinkedList;
impl Solution{
pub fn reconstruct_queue(mut people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut queue = LinkedList::new();
people.sort_by(|a, b| {
if a[0] == b[0] {
return a[1].cmp(&b[1]);
}
b[0].cmp(&a[0])
});
queue.push_back(people[0].clone());
for v in people.iter().skip(1) {
if queue.len() > v[1] as usize {
let mut back_link = queue.split_off(v[1] as usize);
queue.push_back(v.clone());
queue.append(&mut back_link);
} else {
queue.push_back(v.clone());
}
}
queue.into_iter().collect()
}
}
```
Go Go