Skip to content

Commit 7be0bdc

Browse files
Update
1 parent f9820d6 commit 7be0bdc

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ LeetCode 最强题解(持续更新中):
1313
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
1414
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md) |数组 |中等|**模拟**|
1515
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
16-
|[0142.环形链表II.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**|
16+
|[0142.环形链表II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0142.环形链表II.md) |链表 |中等|**模拟**|
17+
|[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**|
1718
|[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**|
1819
|[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md) |链表 |简单| **模拟** **递归**|
1920
|[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 |中等| **暴力** **滑动窗口**|
2021
|[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md) | 哈希表 |简单| **哈希** |
2122
|[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归|
23+
|[0242.有效的字母异位词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0242.有效的字母异位词.md) |哈希表 |简单| **哈希**|
2224
|[0349.两个数组的交集](https://github.com/youngyangyang04/leetcode/blob/master/problems/0349.两个数组的交集.md) |哈希表 |简单|**哈希**|
2325
|[0350.两个数组的交集II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0350.两个数组的交集II.md) |哈希表 |简单|**哈希**|
24-
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数**|
26+
|[0383.赎金信](https://github.com/youngyangyang04/leetcode/blob/master/problems/0383.赎金信.md) |数组 |简单|**暴力** **字典计数** **哈希**|
2527
|[0454.四数相加II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0454.四数相加II.md) |哈希表 |中等| **哈希**|
2628
|[0575.分糖果.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0575.分糖果.md) |哈希表 |简单|**哈希**|
2729
|[0705.设计哈希集合.md](https://github.com/youngyangyang04/leetcode/blob/master/problems/0705.设计哈希集合.md) |哈希表 |简单|**模拟**|

problems/0202.快乐数.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 题目地址
2+
https://leetcode-cn.com/problems/happy-number/
3+
4+
# 思路
5+
6+
这道题目重点是,题目中说了会 **无限循环**, 那么也就是说 求和的过程中,sum会重复出现,这对我们解题很重要,这样我们就可以使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止
7+
8+
还有就是求和的过程,如果对 取数值各个位上的单数操作不熟悉的话,做这道题也会比较艰难
9+
10+
# C++代码
11+
12+
```
13+
class Solution {
14+
public:
15+
// 取和
16+
int getSum(int n) {
17+
int sum = 0;
18+
while (n) {
19+
sum += (n % 10) * (n % 10);
20+
n /= 10;
21+
}
22+
return sum;
23+
}
24+
bool isHappy(int n) {
25+
unordered_set<int> set;
26+
while(1) {
27+
int sum = getSum(n);
28+
if (sum == 1) {
29+
return true;
30+
}
31+
// 如果这个sum曾经出现过,说明已经陷入了无限循环了,立刻return false
32+
if (set.find(sum) != set.end()) {
33+
return false;
34+
} else {
35+
set.insert(sum);
36+
}
37+
n = sum;
38+
}
39+
}
40+
};
41+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
## 题目地址
3+
4+
https://leetcode-cn.com/problems/valid-anagram/
5+
6+
## 思路
7+
8+
使用哈希法来判断 s 中的字母是否 都在t中,且t中的字符也都在s中
9+
10+
## 代码
11+
```
12+
class Solution {
13+
public:
14+
bool isAnagram(string s, string t) {
15+
int record[26] = {0};
16+
for (int i = 0; i < s.size(); i++) {
17+
record[s[i]-'a']++;
18+
}
19+
for (int i = 0; i < t.size(); i++) {
20+
record[t[i]-'a']--;
21+
}
22+
for (int i = 0; i < 26; i++) {
23+
if (record[i] != 0) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
};
30+
```

problems/0350.两个数组的交集II.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ class Solution {
1818
public:
1919
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
2020
vector<int> result;
21-
unordered_map<int, int> umap;
21+
unordered_map<int, int> map;
2222
for (int num : nums1) {
23-
umap[num]++;
23+
map[num]++;
2424
}
2525
for (int num : nums2) {
26-
if (umap[num] > 0) {
26+
if (map[num] > 0) {
2727
result.push_back(num);
28-
umap[num]--;
28+
map[num]--;
2929
}
3030
}
3131
return result;

0 commit comments

Comments
 (0)