mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update
This commit is contained in:
@ -26,8 +26,8 @@
|
|||||||
* [这五道数组相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/vdKHt2vFSZEouZASjdWieg)
|
* [这五道数组相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/vdKHt2vFSZEouZASjdWieg)
|
||||||
* [关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)
|
* [关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)
|
||||||
* [这六道哈希表相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/nxuWv5cUhCPSbAdIHtWgSg)
|
* [这六道哈希表相关的面试题,你一定要会!](https://mp.weixin.qq.com/s/nxuWv5cUhCPSbAdIHtWgSg)
|
||||||
* [关于链表,你该了解这些!](https://mp.weixin.qq.com/s/ntlZbEdKgnFQKZkSUAOSpQ)
|
|
||||||
* [刷leetcode的时候,究竟什么时候可以使用库函数,什么时候不要使用库函数,过来人来说一说](https://leetcode-cn.com/circle/article/E1Kjzn/)
|
* [刷leetcode的时候,究竟什么时候可以使用库函数,什么时候不要使用库函数,过来人来说一说](https://leetcode-cn.com/circle/article/E1Kjzn/)
|
||||||
|
* [关于链表,你该了解这些!](https://mp.weixin.qq.com/s/ntlZbEdKgnFQKZkSUAOSpQ)
|
||||||
* [链表:听说用虚拟头节点会方便很多?](https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA)
|
* [链表:听说用虚拟头节点会方便很多?](https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA)
|
||||||
* [链表:一道题目考察了常见的五个操作!](https://mp.weixin.qq.com/s/Cf95Lc6brKL4g2j8YyF3Mg)
|
* [链表:一道题目考察了常见的五个操作!](https://mp.weixin.qq.com/s/Cf95Lc6brKL4g2j8YyF3Mg)
|
||||||
* [链表:听说过两天反转链表又写不出来了?](https://mp.weixin.qq.com/s/pnvVP-0ZM7epB8y3w_Njwg)
|
* [链表:听说过两天反转链表又写不出来了?](https://mp.weixin.qq.com/s/pnvVP-0ZM7epB8y3w_Njwg)
|
||||||
@ -349,6 +349,8 @@ int countNodes(TreeNode* root) {
|
|||||||
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **双指针/快慢指针/双指针**|
|
|[0027.移除元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md) |数组 |简单| **暴力** **双指针/快慢指针/双指针**|
|
||||||
|[0028.实现strStr()](https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md) |字符串 |简单| **KMP** |
|
|[0028.实现strStr()](https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md) |字符串 |简单| **KMP** |
|
||||||
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 |简单| **暴力** **二分**|
|
|[0035.搜索插入位置](https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md) |数组 |简单| **暴力** **二分**|
|
||||||
|
|[0046.全排列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0046.全排列.md) |回溯|中等| **回溯**|
|
||||||
|
|[0047.全排列II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0047.全排列II.md) |回溯|中等| **回溯**|
|
||||||
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
|
|[0053.最大子序和](https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md) |数组 |简单|**暴力** **贪心** 动态规划 分治|
|
||||||
|[0059.螺旋矩阵II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.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) |链表 |简单|**模拟**|
|
|[0083.删除排序链表中的重复元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md) |链表 |简单|**模拟**|
|
||||||
|
62
problems/0046.全排列.md
Normal file
62
problems/0046.全排列.md
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
## 题目地址
|
||||||
|
https://leetcode-cn.com/problems/permutations/
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
|
先写逻辑,再确认参数,先把for循环写出来,在写 结束语句,在写 函数参数。
|
||||||
|
|
||||||
|
这道题目树形结构还不太一样
|
||||||
|
|
||||||
|
## 解法
|
||||||
|
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> result;
|
||||||
|
void backtracking (vector<int>& nums, vector<int>& vec, vector<bool>& used) {
|
||||||
|
// 此时说明找到了一组
|
||||||
|
if (vec.size() == nums.size()) {
|
||||||
|
result.push_back(vec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.size(); i++) {
|
||||||
|
if (used[i] == false) {
|
||||||
|
used[i] = true;
|
||||||
|
vec.push_back(nums[i]);
|
||||||
|
backtracking(nums, vec, used);
|
||||||
|
vec.pop_back();
|
||||||
|
used[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vector<vector<int>> permute(vector<int>& nums) {
|
||||||
|
vector<bool> used(nums.size(), false);
|
||||||
|
vector<int> vec;
|
||||||
|
backtracking(nums, vec, used);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
这是一个思路:
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> res;
|
||||||
|
vector<vector<int>> permute(vector<int>& nums) {
|
||||||
|
solve(nums, 0);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
void solve(vector<int> &nums, int idx) {
|
||||||
|
if(idx == nums.size()-1 || nums.size() == 0){
|
||||||
|
res.push_back(nums);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(int i = idx; i < nums.size(); i++){
|
||||||
|
swap(nums[idx], nums[i]);
|
||||||
|
solve(nums, idx+1);
|
||||||
|
swap(nums[idx], nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
47
problems/0047.全排列II.md
Normal file
47
problems/0047.全排列II.md
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
## 题目地址
|
||||||
|
https://leetcode-cn.com/problems/permutations-ii/
|
||||||
|
|
||||||
|
## 思路
|
||||||
|
|
||||||
|
i > 0 && nums[i] == nums[i-1] && used[i-1] == false
|
||||||
|
|
||||||
|
这是最高效的,可以用 1 1 1 1 1 跑一个样例试试
|
||||||
|
|
||||||
|
## C++代码
|
||||||
|
|
||||||
|
```
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
vector<vector<int>> result;
|
||||||
|
void backtracking (vector<int>& nums, vector<int>& vec, vector<bool>& used) {
|
||||||
|
// 此时说明找到了一组
|
||||||
|
if (vec.size() == nums.size()) {
|
||||||
|
result.push_back(vec);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < nums.size(); i++) {
|
||||||
|
if (i > 0 && nums[i] == nums[i-1] && used[i-1] == false) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (used[i] == false) {
|
||||||
|
used[i] = true;
|
||||||
|
vec.push_back(nums[i]);
|
||||||
|
backtracking(nums, vec, used);
|
||||||
|
vec.pop_back();
|
||||||
|
used[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
vector<vector<int>> permuteUnique(vector<int>& nums) {
|
||||||
|
sort(nums.begin(), nums.end());
|
||||||
|
vector<bool> used(nums.size(), false);
|
||||||
|
vector<int> vec;
|
||||||
|
backtracking(nums, vec, used);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
@ -47,3 +47,4 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||||
|
@ -122,5 +122,5 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
|
||||||
|
|
||||||
|
@ -1,13 +1,35 @@
|
|||||||
# 题目地址
|
# 题目地址
|
||||||
https://leetcode-cn.com/problems/happy-number/
|
https://leetcode-cn.com/problems/happy-number/
|
||||||
|
|
||||||
|
> 该用set的时候,还是得用set
|
||||||
|
|
||||||
|
# 第202题. 快乐数
|
||||||
|
|
||||||
|
编写一个算法来判断一个数 n 是不是快乐数。
|
||||||
|
|
||||||
|
「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。
|
||||||
|
|
||||||
|
如果 n 是快乐数就返回 True ;不是,则返回 False 。
|
||||||
|
|
||||||
|
**示例:**
|
||||||
|
|
||||||
|
输入:19
|
||||||
|
输出:true
|
||||||
|
解释:
|
||||||
|
12 + 92 = 82
|
||||||
|
82 + 22 = 68
|
||||||
|
62 + 82 = 100
|
||||||
|
12 + 02 + 02 = 1
|
||||||
|
|
||||||
# 思路
|
# 思路
|
||||||
|
|
||||||
这道题目看上去貌似一道数学问题,其实它也需要使用哈希法!
|
这道题目看上去貌似一道数学问题,其实并不是!
|
||||||
|
|
||||||
这道题目重点是,题目中说了会 **无限循环**,那么也就是说**求和的过程中,sum会重复出现,这对解题很重要!**
|
题目中说了会 **无限循环**,那么也就是说**求和的过程中,sum会重复出现,这对解题很重要!**
|
||||||
|
|
||||||
这样就可以使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
|
正如:[关于哈希表,你该了解这些!](https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA)中所说,**当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。**
|
||||||
|
|
||||||
|
所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
|
||||||
|
|
||||||
判断sum是否重复出现就可以使用unordered_set。
|
判断sum是否重复出现就可以使用unordered_set。
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user