-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 第三章 哈希表part02 | ||
|
||
今日任务 | ||
|
||
● 454.四数相加II | ||
● 383. 赎金信 | ||
● 15. 三数之和 | ||
● 18. 四数之和 | ||
● 总结 | ||
|
||
详细布置 | ||
|
||
## 454. 四数相加II | ||
|
||
建议:本题是 使用map 巧妙解决的问题,好好体会一下 哈希法 如何提高程序执行效率,降低时间复杂度,当然使用哈希法 会提高空间复杂度,但一般来说我们都是舍空间 换时间, 工业开发也是这样。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0454.%E5%9B%9B%E6%95%B0%E7%9B%B8%E5%8A%A0II.html | ||
|
||
|
||
## 383. 赎金信 | ||
|
||
建议:本题 和 242.有效的字母异位词 是一个思路 ,算是拓展题 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html | ||
|
||
|
||
## 15. 三数之和 | ||
|
||
建议:本题虽然和 两数之和 很像,也能用哈希法,但用哈希法会很麻烦,双指针法才是正解,可以先看视频理解一下 双指针法的思路,文章中讲解的,没问题 哈希法很麻烦。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0015.%E4%B8%89%E6%95%B0%E4%B9%8B%E5%92%8C.html | ||
|
||
## 18. 四数之和 | ||
|
||
建议: 要比较一下,本题和 454.四数相加II 的区别,为什么 454.四数相加II 会简单很多,这个想明白了,对本题理解就深刻了。 本题 思路整体和 三数之和一样的,都是双指针,但写的时候 有很多小细节,需要注意,建议先看视频。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0018.%E5%9B%9B%E6%95%B0%E4%B9%8B%E5%92%8C.html | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# 15. 三数之和 | ||
|
||
## 题目描述 | ||
|
||
给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请 | ||
|
||
你返回所有和为 0 且不重复的三元组。 | ||
|
||
注意:答案中不可以包含重复的三元组。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn three_sum(mut v: Vec<i32>) -> Vec<Vec<i32>> { | ||
v.sort(); | ||
let mut ans = vec![]; | ||
for i in 0..v.len() - 2 { | ||
if i > 0 && v[i] == v[i - 1] { | ||
continue; | ||
} | ||
let mut j = i + 1; | ||
let mut k = v.len() - 1; | ||
while j < k { | ||
if v[j] + v[k] + v[i] == 0 { | ||
ans.push(vec![v[i], v[j], v[k]]); | ||
j += 1; | ||
while j < k && v[j] == v[j - 1] { | ||
j += 1 | ||
} | ||
k -= 1; | ||
while j < k && v[k] == v[k + 1] { | ||
k -= 1 | ||
} | ||
} else if v[j] + v[k] < -v[i] { | ||
j += 1 | ||
} else { | ||
k -= 1 | ||
} | ||
} | ||
} | ||
ans | ||
} | ||
} | ||
``` | ||
|
||
双指针做法还是想不到 | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 18. 四数之和 | ||
|
||
## 题目描述 | ||
|
||
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复): | ||
|
||
0 <= a, b, c, d < n | ||
a、b、c 和 d 互不相同 | ||
nums[a] + nums[b] + nums[c] + nums[d] == target | ||
你可以按 任意顺序 返回答案 。 | ||
|
||
## 解题思路 | ||
|
||
有两个坑: | ||
|
||
- 首先是n < 4的情况是存在的 | ||
- 其次四个数的加和可以超过i32的最大值 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn four_sum(mut v: Vec<i32>, t: i32) -> Vec<Vec<i32>> { | ||
v.sort(); | ||
let n = v.len(); | ||
let mut res = vec![]; | ||
if n < 4 { return res } | ||
for i in 0..n-3 { | ||
if i > 0 && v[i] == v[i-1] { continue } | ||
for l in (i+3..n).rev() { | ||
if l < n - 1 && v[l] == v[l+1] { continue } | ||
let mut j = i + 1; | ||
let mut k = l - 1; | ||
while j < k { | ||
if v[i] as isize + v[j] as isize + v[k] as isize + v[l] as isize == t as isize { | ||
res.push(vec![v[i], v[j], v[k], v[l]]); | ||
j += 1; | ||
while j < k && v[j] == v[j-1] { j += 1 } | ||
k -= 1; | ||
while j < k && v[k] == v[k+1] { k -= 1 } | ||
} else if (v[i] as isize + v[j] as isize + v[k] as isize + v[l] as isize) < t as isize { | ||
j += 1; | ||
} else { | ||
k -= 1; | ||
} | ||
} | ||
} | ||
} | ||
res | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 383. 赎金信 | ||
|
||
## 题目描述 | ||
|
||
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 | ||
|
||
如果可以,返回 true ;否则返回 false 。 | ||
|
||
magazine 中的每个字符只能在 ransomNote 中使用一次。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
|
||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn can_construct(ransom_note: String, magazine: String) -> bool { | ||
use std::collections::HashMap; | ||
let mut map: HashMap<char, usize> = HashMap::new(); | ||
for c in magazine.chars() { | ||
*map.entry(c).or_default() += 1; | ||
} | ||
for c in ransom_note.chars() { | ||
if ! map.contains_key(&c) { return false } | ||
let a = map.get_mut(&c).unwrap(); | ||
*a -= 1; | ||
if *a == 0 { map.remove(&c); } | ||
} | ||
true | ||
} | ||
} | ||
``` | ||
|
||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 454. 四数相加 II | ||
|
||
## 题目描述 | ||
|
||
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: | ||
|
||
0 <= i, j, k, l < n | ||
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0 | ||
|
||
|
||
## 解题思路 | ||
|
||
首先是一种愚蠢的做法,以为On2就是两个for循环 | ||
|
||
```rust | ||
struct Solution {} | ||
|
||
impl Solution { | ||
pub fn four_sum_count(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>, nums4: Vec<i32>) -> i32 { | ||
let n = nums1.len(); | ||
use std::collections::HashMap; | ||
let mut res = 0; | ||
let mut map3: HashMap<i32, usize> = HashMap::new(); | ||
let mut map4: HashMap<i32, usize> = HashMap::new(); | ||
for i in 0..n { | ||
*map3.entry(nums3[i]).or_default() += 1; | ||
*map4.entry(nums4[i]).or_default() += 1; | ||
} | ||
for i in 0..n { | ||
for j in 0..n { | ||
let mut target = - nums1[i] - nums2[j]; | ||
for (&a, &b) in map3.iter() { | ||
let d = target - a; | ||
if map4.contains_key(&d) { | ||
res += b * map4.get(&d).unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
res as i32 | ||
} | ||
} | ||
``` | ||
|
||
这里面白白浪费了i和j的和 | ||
|
||
## 学习感想 | ||
|
||
```rust | ||
|
||
|
||
struct Solution {} | ||
|
||
|
||
impl Solution { | ||
pub fn four_sum_count(nums1: Vec<i32>, nums2: Vec<i32>, nums3: Vec<i32>, nums4: Vec<i32>) -> i32 { | ||
let n = nums1.len(); | ||
use std::collections::HashMap; | ||
let mut res = 0; | ||
let mut map: HashMap<i32, usize> = HashMap::new(); | ||
for i in 0..n { | ||
for j in 0..n { | ||
let target = nums1[i] + nums2[j]; | ||
*map.entry(target).or_default() += 1; | ||
} | ||
} | ||
for i in 0..n { | ||
for j in 0..n { | ||
let target = - nums3[i] - nums4[j]; | ||
if map.contains_key(&target) { | ||
res += map.get(&target).unwrap(); | ||
} | ||
} | ||
} | ||
res as i32 | ||
} | ||
} | ||
``` |
Empty file.