From dbae949f8a7f74194bb2b4aed2bba02e0a76d2f3 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 30 Jun 2020 11:00:26 +0800 Subject: [PATCH] Update --- README.md | 6 ++-- problems/0202.快乐数.md | 41 +++++++++++++++++++++++ problems/0242.有效的字母异位词.md | 30 +++++++++++++++++ problems/0350.两个数组的交集II.md | 8 ++--- 4 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 problems/0202.快乐数.md create mode 100644 problems/0242.有效的字母异位词.md diff --git a/README.md b/README.md index 20ffcb84..4de7662b 100644 --- a/README.md +++ b/README.md @@ -13,15 +13,17 @@ LeetCode 最强题解(持续更新中): |[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治| |[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**| |[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**| -|[0142.环形链表II.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**| +|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**| +|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**| |[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**| |[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md) |链表 |简单| **模拟** **递归**| |[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 |中等| **暴力** **滑动窗口**| |[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md) | 哈希表 |简单| **哈希** | |[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归| +|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.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) |数组 |简单|**暴力** **字典计数**| +|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**| |[0454.四数相加II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.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/0202.快乐数.md b/problems/0202.快乐数.md new file mode 100644 index 00000000..60c534c4 --- /dev/null +++ b/problems/0202.快乐数.md @@ -0,0 +1,41 @@ +# 题目地址 +https://leetcode-cn.com/problems/happy-number/ + +# 思路 + +这道题目重点是,题目中说了会 **无限循环**, 那么也就是说 求和的过程中,sum会重复出现,这对我们解题很重要,这样我们就可以使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止 + +还有就是求和的过程,如果对 取数值各个位上的单数操作不熟悉的话,做这道题也会比较艰难 + +# C++代码 + +``` +class Solution { +public: + // 取和 + int getSum(int n) { + int sum = 0; + while (n) { + sum += (n % 10) * (n % 10); + n /= 10; + } + return sum; + } + bool isHappy(int n) { + unordered_set set; + while(1) { + int sum = getSum(n); + if (sum == 1) { + return true; + } + // 如果这个sum曾经出现过,说明已经陷入了无限循环了,立刻return false + if (set.find(sum) != set.end()) { + return false; + } else { + set.insert(sum); + } + n = sum; + } + } +}; +``` diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md new file mode 100644 index 00000000..425cb805 --- /dev/null +++ b/problems/0242.有效的字母异位词.md @@ -0,0 +1,30 @@ + +## 题目地址 + +https://leetcode-cn.com/problems/valid-anagram/ + +## 思路 + +使用哈希法来判断 s 中的字母是否 都在t中,且t中的字符也都在s中 + +## 代码 +``` +class Solution { +public: + bool isAnagram(string s, string t) { + int record[26] = {0}; + for (int i = 0; i < s.size(); i++) { + record[s[i]-'a']++; + } + for (int i = 0; i < t.size(); i++) { + record[t[i]-'a']--; + } + for (int i = 0; i < 26; i++) { + if (record[i] != 0) { + return false; + } + } + return true; + } +}; +``` diff --git a/problems/0350.两个数组的交集II.md b/problems/0350.两个数组的交集II.md index 68c2d2a1..f3cf2b19 100644 --- a/problems/0350.两个数组的交集II.md +++ b/problems/0350.两个数组的交集II.md @@ -18,14 +18,14 @@ class Solution { public: vector intersect(vector& nums1, vector& nums2) { vector result; - unordered_map umap; + unordered_map map; for (int num : nums1) { - umap[num]++; + map[num]++; } for (int num : nums2) { - if (umap[num] > 0) { + if (map[num] > 0) { result.push_back(num); - umap[num]--; + map[num]--; } } return result;