mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -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) |哈希表 |简单|**模拟**|
|
||||
|
41
problems/0202.快乐数.md
Normal file
41
problems/0202.快乐数.md
Normal file
@ -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<int> 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;
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
30
problems/0242.有效的字母异位词.md
Normal file
30
problems/0242.有效的字母异位词.md
Normal file
@ -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;
|
||||
}
|
||||
};
|
||||
```
|
@ -18,14 +18,14 @@ class Solution {
|
||||
public:
|
||||
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
|
||||
vector<int> result;
|
||||
unordered_map<int, int> umap;
|
||||
unordered_map<int, int> 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;
|
||||
|
Reference in New Issue
Block a user