-
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
193 additions
and
0 deletions.
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,3 @@ | ||
# 休息日 | ||
|
||
休息日我用来补进度了 |
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,49 @@ | ||
# 第三章 哈希表part01 | ||
|
||
今日任务 | ||
|
||
● 哈希表理论基础 | ||
● 242.有效的字母异位词 | ||
● 349. 两个数组的交集 | ||
● 202. 快乐数 | ||
● 1. 两数之和 | ||
|
||
详细布置 | ||
|
||
## 哈希表理论基础 | ||
|
||
建议:大家要了解哈希表的内部实现原理,哈希函数,哈希碰撞,以及常见哈希表的区别,数组,set 和map。 | ||
|
||
什么时候想到用哈希法,当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法。 这句话很重要,大家在做哈希表题目都要思考这句话。 | ||
|
||
文章讲解:https://programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html | ||
|
||
|
||
## 242.有效的字母异位词 | ||
|
||
建议: 这道题目,大家可以感受到 数组 用来做哈希表 给我们带来的遍历之处。 | ||
|
||
题目链接/文章讲解/视频讲解: https://programmercarl.com/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.html | ||
|
||
|
||
## 349. 两个数组的交集 | ||
|
||
建议:本题就开始考虑 什么时候用set 什么时候用数组,本题其实是使用set的好题,但是后来力扣改了题目描述和 测试用例,添加了 0 <= nums1[i], nums2[i] <= 1000 条件,所以使用数组也可以了,不过建议大家忽略这个条件。 尝试去使用set。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0349.%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86.html | ||
|
||
## 202. 快乐数 | ||
|
||
建议:这道题目也是set的应用,其实和上一题差不多,就是 套在快乐数一个壳子 | ||
|
||
题目链接/文章讲解:https://programmercarl.com/0202.%E5%BF%AB%E4%B9%90%E6%95%B0.html | ||
|
||
## 1. 两数之和 | ||
|
||
建议:本题虽然是 力扣第一题,但是还是挺难的,也是 代码随想录中 数组,set之后,使用map解决哈希问题的第一题。 | ||
|
||
建议大家先看视频讲解,然后尝试自己写代码,在看文章讲解,加深印象。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0001.%E4%B8%A4%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,36 @@ | ||
# 1. 两数之和 | ||
|
||
## 题目描述 | ||
|
||
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 | ||
|
||
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 | ||
|
||
你可以按任意顺序返回答案。 | ||
|
||
|
||
|
||
## 解题思路 | ||
|
||
```rust | ||
struct Solution {} | ||
|
||
|
||
impl Solution { | ||
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
use std::collections::HashMap; | ||
let mut map: HashMap<i32, i32> = HashMap::new(); | ||
for (idx, i) in nums.iter().enumerate() { | ||
if map.contains_key(&(target - i)) { | ||
return vec![*map.get(&(target - i)).unwrap(), idx as i32] | ||
} else { | ||
map.insert(*i, idx as i32); | ||
} | ||
} | ||
todo!() | ||
} | ||
} | ||
``` | ||
|
||
|
||
## 学习感想 |
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,42 @@ | ||
# 202. 快乐数 | ||
|
||
## 题目描述 | ||
|
||
编写一个算法来判断一个数 n 是不是快乐数。 | ||
|
||
「快乐数」 定义为: | ||
|
||
对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。 | ||
然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。 | ||
如果这个过程 结果为 1,那么这个数就是快乐数。 | ||
如果 n 是 快乐数 就返回 true ;不是,则返回 false 。 | ||
|
||
## 解题思路 | ||
|
||
```rust | ||
impl Solution { | ||
pub fn is_happy(mut n: i32) -> bool { | ||
use std::collections::HashSet; | ||
let mut set = HashSet::new(); | ||
set.insert(n); | ||
fn f(mut n: i32) -> i32 { | ||
let mut res = 0; | ||
while n != 0 { | ||
let x = n % 10; | ||
res += x * x; | ||
n /= 10; | ||
} | ||
res | ||
} | ||
loop { | ||
n = f(n); | ||
if n == 1 { return true } | ||
if set.contains(&n) { return false } | ||
set.insert(n); | ||
} | ||
} | ||
} | ||
``` | ||
## 学习感想 | ||
|
||
一下子不知道怎么做,但是把false的例子弄明白就知道了 |
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,32 @@ | ||
# 242. 有效的字母异位词 | ||
|
||
## 题目描述 | ||
|
||
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 | ||
|
||
注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。 | ||
|
||
## 解题思路 | ||
```rust | ||
|
||
# struct Solution {} | ||
impl Solution { | ||
pub fn is_anagram(s: String, t: String) -> bool { | ||
use std::collections::HashMap; | ||
if s.len() != t.len() { return false } | ||
let mut freq: HashMap<char, usize> = HashMap::new(); | ||
for c in s.chars() { | ||
*freq.entry(c).or_default() += 1; | ||
} | ||
for c in t.chars() { | ||
let entry = freq.entry(c).or_default(); | ||
if *entry == 0 { return false } | ||
*entry -= 1; | ||
} | ||
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,26 @@ | ||
# 349. 两个数组的交集 | ||
## 题目描述 | ||
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。 | ||
## 解题思路 | ||
|
||
```rust | ||
|
||
# struct Solution {} | ||
|
||
impl Solution { | ||
pub fn intersection(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> { | ||
use std::collections::HashSet; | ||
let mut set1 = HashSet::new(); | ||
let mut set2 = HashSet::new(); | ||
for i in nums1 { | ||
set1.insert(i); | ||
} | ||
for i in nums2 { | ||
set2.insert(i); | ||
} | ||
set1.intersection(&set2).copied().collect() | ||
} | ||
} | ||
|
||
``` | ||
## 学习感想 |