File tree Expand file tree Collapse file tree 6 files changed +139
-5
lines changed Expand file tree Collapse file tree 6 files changed +139
-5
lines changed Original file line number Diff line number Diff line change 2626* [ 这五道数组相关的面试题,你一定要会!] ( https://mp.weixin.qq.com/s/vdKHt2vFSZEouZASjdWieg )
2727* [ 关于哈希表,你该了解这些!] ( https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA )
2828* [ 这六道哈希表相关的面试题,你一定要会!] ( https://mp.weixin.qq.com/s/nxuWv5cUhCPSbAdIHtWgSg )
29- * [ 关于链表,你该了解这些!] ( https://mp.weixin.qq.com/s/ntlZbEdKgnFQKZkSUAOSpQ )
3029* [ 刷leetcode的时候,究竟什么时候可以使用库函数,什么时候不要使用库函数,过来人来说一说] ( https://leetcode-cn.com/circle/article/E1Kjzn/ )
30+ * [ 关于链表,你该了解这些!] ( https://mp.weixin.qq.com/s/ntlZbEdKgnFQKZkSUAOSpQ )
3131* [ 链表:听说用虚拟头节点会方便很多?] ( https://mp.weixin.qq.com/s/slM1CH5Ew9XzK93YOQYSjA )
3232* [ 链表:一道题目考察了常见的五个操作!] ( https://mp.weixin.qq.com/s/Cf95Lc6brKL4g2j8YyF3Mg )
3333* [ 链表:听说过两天反转链表又写不出来了?] ( https://mp.weixin.qq.com/s/pnvVP-0ZM7epB8y3w_Njwg )
@@ -349,6 +349,8 @@ int countNodes(TreeNode* root) {
349349| [ 0027.移除元素] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0027.移除元素.md ) | 数组 | 简单| ** 暴力** ** 双指针/快慢指针/双指针** |
350350| [ 0028.实现strStr()] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0028.实现strStr().md ) | 字符串 | 简单| ** KMP** |
351351| [ 0035.搜索插入位置] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0035.搜索插入位置.md ) | 数组 | 简单| ** 暴力** ** 二分** |
352+ | [ 0046.全排列] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0046.全排列.md ) | 回溯| 中等| ** 回溯** |
353+ | [ 0047.全排列II] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0047.全排列II.md ) | 回溯| 中等| ** 回溯** |
352354| [ 0053.最大子序和] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0053.最大子序和.md ) | 数组 | 简单| ** 暴力** ** 贪心** 动态规划 分治|
353355| [ 0059.螺旋矩阵II] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0059.螺旋矩阵II.md ) | 数组 | 中等| ** 模拟** |
354356| [ 0083.删除排序链表中的重复元素] ( https://github.com/youngyangyang04/leetcode/blob/master/problems/0083.删除排序链表中的重复元素.md ) | 链表 | 简单| ** 模拟** |
Original file line number Diff line number Diff line change 1+
2+ ## 题目地址
3+ https://leetcode-cn.com/problems/permutations/
4+
5+ ## 思路
6+
7+ 先写逻辑,再确认参数,先把for循环写出来,在写 结束语句,在写 函数参数。
8+
9+ 这道题目树形结构还不太一样
10+
11+ ## 解法
12+
13+ ```
14+ class Solution {
15+ public:
16+ vector<vector<int>> result;
17+ void backtracking (vector<int>& nums, vector<int>& vec, vector<bool>& used) {
18+ // 此时说明找到了一组
19+ if (vec.size() == nums.size()) {
20+ result.push_back(vec);
21+ return;
22+ }
23+
24+ for (int i = 0; i < nums.size(); i++) {
25+ if (used[i] == false) {
26+ used[i] = true;
27+ vec.push_back(nums[i]);
28+ backtracking(nums, vec, used);
29+ vec.pop_back();
30+ used[i] = false;
31+ }
32+ }
33+ }
34+ vector<vector<int>> permute(vector<int>& nums) {
35+ vector<bool> used(nums.size(), false);
36+ vector<int> vec;
37+ backtracking(nums, vec, used);
38+ return result;
39+ }
40+ };
41+ ```
42+
43+ 这是一个思路:
44+ class Solution {
45+ public:
46+ vector<vector<int >> res;
47+ vector<vector<int >> permute(vector<int >& nums) {
48+ solve(nums, 0);
49+ return res;
50+ }
51+ void solve(vector<int > &nums, int idx) {
52+ if(idx == nums.size()-1 || nums.size() == 0){
53+ res.push_back(nums);
54+ return;
55+ }
56+ for(int i = idx; i < nums.size(); i++){
57+ swap(nums[ idx] , nums[ i] );
58+ solve(nums, idx+1);
59+ swap(nums[ idx] , nums[ i] );
60+ }
61+ }
62+ };
Original file line number Diff line number Diff line change 1+ ## 题目地址
2+ https://leetcode-cn.com/problems/permutations-ii/
3+
4+ ## 思路
5+
6+ i > 0 && nums[ i] == nums[ i-1] && used[ i-1] == false
7+
8+ 这是最高效的,可以用 1 1 1 1 1 跑一个样例试试
9+
10+ ## C++代码
11+
12+ ```
13+ class Solution {
14+ private:
15+ vector<vector<int>> result;
16+ void backtracking (vector<int>& nums, vector<int>& vec, vector<bool>& used) {
17+ // 此时说明找到了一组
18+ if (vec.size() == nums.size()) {
19+ result.push_back(vec);
20+ return;
21+ }
22+
23+ for (int i = 0; i < nums.size(); i++) {
24+ if (i > 0 && nums[i] == nums[i-1] && used[i-1] == false) {
25+ continue;
26+ }
27+ if (used[i] == false) {
28+ used[i] = true;
29+ vec.push_back(nums[i]);
30+ backtracking(nums, vec, used);
31+ vec.pop_back();
32+ used[i] = false;
33+ }
34+ }
35+ }
36+
37+ public:
38+ vector<vector<int>> permuteUnique(vector<int>& nums) {
39+ sort(nums.begin(), nums.end());
40+ vector<bool> used(nums.size(), false);
41+ vector<int> vec;
42+ backtracking(nums, vec, used);
43+ return result;
44+
45+ }
46+ };
47+ ```
Original file line number Diff line number Diff line change @@ -47,3 +47,4 @@ public:
4747 }
4848};
4949```
50+ > 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
Original file line number Diff line number Diff line change @@ -122,5 +122,5 @@ public:
122122 }
123123};
124124```
125- > 更过算法干货文章持续更新 ,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
125+ > 更多算法干货文章持续更新 ,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。
126126
Original file line number Diff line number Diff line change 11# 题目地址
22https://leetcode-cn.com/problems/happy-number/
33
4+ > 该用set的时候,还是得用set
5+
6+ # 第202题. 快乐数
7+
8+ 编写一个算法来判断一个数 n 是不是快乐数。
9+
10+ 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。
11+
12+ 如果 n 是快乐数就返回 True ;不是,则返回 False 。
13+
14+ ** 示例:**
15+
16+ 输入:19
17+ 输出:true
18+ 解释:
19+ 12 + 92 = 82
20+ 82 + 22 = 68
21+ 62 + 82 = 100
22+ 12 + 02 + 02 = 1
23+
424# 思路
525
6- 这道题目看上去貌似一道数学问题,其实它也需要使用哈希法!
26+ 这道题目看上去貌似一道数学问题,其实并不是!
27+
28+ 题目中说了会 ** 无限循环** ,那么也就是说** 求和的过程中,sum会重复出现,这对解题很重要!**
729
8- 这道题目重点是,题目中说了会 ** 无限循环 ** ,那么也就是说 ** 求和的过程中,sum会重复出现,这对解题很重要! **
30+ 正如: [ 关于哈希表,你该了解这些! ] ( https://mp.weixin.qq.com/s/g8N6WmoQmsCUw3_BaWxHZA ) 中所说, ** 当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。 **
931
10- 这样就可以使用哈希法 ,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
32+ 所以这道题目使用哈希法 ,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
1133
1234判断sum是否重复出现就可以使用unordered_set。
1335
You can’t perform that action at this time.
0 commit comments