diff --git a/README.md b/README.md index 5edb20b8..b8d666a8 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,15 @@ > ACM亚洲区域赛铜牌获得者,哈工大计算机硕士毕业后,先后在腾讯和百度工作多年,对算法和后端技术有一定的见解,利用工作之余重新刷leetcode > 欢迎关注微信公众号:「代码随想录」,这里将持续分享自己对互联网以及技术的想法与思考 +leetcode精选: +[精选数组相关的面试题](https://mp.weixin.qq.com/s?__biz=Mzg5MTExMTk2OA==&mid=2247484000&idx=1&sn=48e87c8ca22390ac5e78bcbb45e2052c&chksm=cfd31d2ff8a4943916305f519d963d1c0979f112abd6f613af7bdb7fa6ec3f86e8c93c2e0caa&token=411961669&lang=zh_CN#rd) +[精选链表相关的面试题]() + LeetCode 最强题解(持续更新中): |题目 | 类型 | 解题方法 | |---|---| ---| -|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.md) | 数组|**暴力** **哈希**| +|[0000.两数之和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0000.两数之和.md) | 数组|**暴力** **哈希**| |[0021.合并两个有序链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0021.合并两个有序链表.md) |链表 |**模拟** | |[0026.删除排序数组中的重复项](https://github.com/youngyangyang04/leetcode/blob/master/problems/0026.删除排序数组中的重复项.md) |数组 |**暴力** **快慢指针** | |[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 | **暴力** **快慢指针**| @@ -20,6 +24,8 @@ LeetCode 最强题解(持续更新中): |[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md) |链表 | **模拟** **递归**| |[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 | **暴力** **滑动窗口**| |[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 | **原链表移除** **添加虚拟节点** 递归| +|[0349.两个数组的交集](https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md) |哈希表 |**哈希**| +|[0350.两个数组的交集II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md) |哈希表 |**哈希**| |[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |**暴力** **字典计数**| |[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |**哈希**| |[0705.设计哈希集合.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |**模拟**| diff --git a/problems/0000.两数之和.md b/problems/0000.两数之和.md index 6547032c..c2671972 100644 --- a/problems/0000.两数之和.md +++ b/problems/0000.两数之和.md @@ -63,3 +63,5 @@ public: } }; ``` + +> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md new file mode 100644 index 00000000..8bc3ac0c --- /dev/null +++ b/problems/0015.三数之和.md @@ -0,0 +1,15 @@ +## 题目地址 + +## 思路 + +## 哈希解法 + +去重的过程不好处理,有很多小细节,在面试中很难想到为 + +## 双指针 + +推荐使用这个方法,排序后用双指针前后操作,比较容易达到去重的目的 + +O(n^2) + +> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0021.合并两个有序链表.md b/problems/0021.合并两个有序链表.md index d9b1b367..f9f8185f 100644 --- a/problems/0021.合并两个有序链表.md +++ b/problems/0021.合并两个有序链表.md @@ -45,3 +45,5 @@ public: } }; ``` + +> 更多精彩文章持续更新,可以微信搜索「 代码随想录」第一时间阅读,关注后有大量的学习资料和简历模板可以免费领取,本文 [GitHub](https://github.com/youngyangyang04/leetcode-master ):https://github.com/youngyangyang04/leetcode-master 已经收录,欢迎star,fork,共同学习,一起进步。 diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md new file mode 100644 index 00000000..e30b7ae2 --- /dev/null +++ b/problems/0349.两个数组的交集.md @@ -0,0 +1,24 @@ + +## 题目地址 +https://leetcode-cn.com/problems/intersection-of-two-arrays/ + +## 思路 + +这道题目,主要要学会使用一种哈希数据结构,unordered_set,这个数据结构可以解决很多类似的问题 + +## 代码 +``` +class Solution { +public: + vector intersection(vector& nums1, vector& nums2) { + unordered_set set; // 存放结果 + unordered_set nums_set(nums1.begin(), nums1.end()); + for (int num : nums2) { + if (nums_set.find(num) != nums_set.end()) { // 发现nums1与nums2有重复 + set.insert(num); + } + } + return vector(set.begin(), set.end()); + } +}; +``` diff --git a/problems/0350.两个数组的交集II.md b/problems/0350.两个数组的交集II.md new file mode 100644 index 00000000..68c2d2a1 --- /dev/null +++ b/problems/0350.两个数组的交集II.md @@ -0,0 +1,34 @@ + +## 题目地址 + +https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/ + +## 思路 + +这道题目,看上去和349两个数组的交集题目描述是一样的,其实这两道题解法相差还是很大的,编号349题目结果是去重的 + + +而本题才求的真正的交集,求这两个集合元素的交集,需要掌握另一个哈希数据结构unordered_map + + +## 代码 + +``` +class Solution { +public: + vector intersect(vector& nums1, vector& nums2) { + vector result; + unordered_map umap; + for (int num : nums1) { + umap[num]++; + } + for (int num : nums2) { + if (umap[num] > 0) { + result.push_back(num); + umap[num]--; + } + } + return result; + } +}; +```