Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3fe6a59

Browse files
committed
新增回溯算法
1 parent a61c553 commit 3fe6a59

File tree

13 files changed

+737
-1
lines changed

13 files changed

+737
-1
lines changed

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,22 @@
3939
| 55 | [跳跃游戏](https://leetcode-cn.com/problems/jump-game/) | [jump_game](./greed/leetcode/medium/jump_game.h) | <font color=orange> medium </font> ||
4040
| 56 | [合并区间](https://leetcode-cn.com/problems/merge-intervals/) | [merge_intervals](./sort/leetcode/merge_intervals.h) | <font color=orange> medium </font> ||
4141
| 58| [最后一个单词的长度](https://leetcode-cn.com/problems/length-of-last-word) | [lengthOfLastWord](./string/leetcode/easy/lengthOfLastWord.h) | <font color=green>easy</font> ||
42+
| 60 | [第k个排列](https://leetcode-cn.com/problems/permutation-sequence/)| [permutation_sequence](./backtracking/leetcode/medium/permutation_sequence.h) | <font color=orange> medium </font> ||
4243
| 66 | [加一](https://leetcode-cn.com/problems/plus-one/) | [plusOne](./array/leetcode/easy/plusOne.h) | <font color=green>easy</font> ||
4344
| 67 | [二进制求和](https://leetcode-cn.com/problems/add-binary/) | [addBinary](./array/leetcode/easy/addBinary.h) | <font color=green>easy</font> ||
4445
| 69 | [x 的平方根](https://leetcode-cn.com/problems/sqrtx/%E2%80%A8)| [mySqrt](./bsearch/leetcode/mySqrt.h) | <font color=green>easy</font> ||
4546
| 70 | [爬楼梯](https://leetcode-cn.com/problems/climbing-stairs/)| [climbStairs](./dp/leetcode/easy/climbStairs.h) | <font color=green>easy</font> ||
4647
| 74 | [搜索二维矩阵](https://leetcode-cn.com/problems/search-a-2d-matrix/) | [search_a_2d_matrix](./bsearch/leetcode/medium/search_a_2d_matrix.h) | <font color=orange> medium </font> ||
4748
| 75 | [颜色分类](https://leetcode-cn.com/problems/sort-colors/) | [sort_colors](./sort/leetcode/sort_colors.h) | <font color=orange> medium </font> ||
49+
| 77 | [组合](https://leetcode-cn.com/problems/combinations/)| [combinations](./backtracking/leetcode/medium/combinations.h) | <font color=orange> medium </font> ||
50+
| 78 | [子集](https://leetcode-cn.com/problems/subsets/)| [subsets](./backtracking/leetcode/medium/subsets.h) | <font color=orange> medium </font> ||
51+
| 79 | [单词搜索](https://leetcode-cn.com/problems/word-search/)| [word_search](./backtracking/leetcode/medium/word_search.h) | <font color=orange> medium </font> ||
4852
| 81 | [搜索旋转排序数组II](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/) | [searchInRotatedSortedArrayII](./bsearch/leetcode/medium/searchInRotatedSortedArrayII.h) | <font color=orange> medium </font> ||
4953
| 83 | [删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/)| [deleteDuplicates](./linkedList/leetcode/easy/deleteDuplicates.h) | <font color=green>easy</font> ||
5054
| 88 | [合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) | [merge](./array/leetcode/easy/merge.h) | <font color=green>easy</font> ||
55+
| 89 | [格雷编码](https://leetcode-cn.com/problems/gray-code/)| [gray_code](./backtracking/leetcode/medium/gray_code.h) | <font color=orange> medium </font> ||
56+
| 90 | [子集II](https://leetcode-cn.com/problems/subsets-ii)| [subsets_ii](./backtracking/leetcode/medium/subsets_ii.h) | <font color=orange> medium </font> ||
57+
| 93 | [复原IP地址](https://leetcode-cn.com/problems/restore-ip-addresses)| [restore_ip_addresses](./backtracking/leetcode/medium/restore_ip_addresses.h) | <font color=orange> medium </font> ||
5158
| 100 | [相同的树](https://leetcode-cn.com/problems/same-tree/) | [isSameTree](./tree/leetcode/easy/isSameTree.h) | <font color=green>easy</font> ||
5259
| 134 | [加油站](https://leetcode-cn.com/problems/gas-station/) | [gas_station](./greed/leetcode/medium/gas_station.h) | <font color=orange> medium </font> ||
5360
| 153 | [寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) | [find_minimum_in_rotated_sorted_array](./bsearch/leetcode/medium/find_minimum_in_rotated_sorted_array.h) | <font color=orange> medium </font> ||

‎alg-cpp.xcodeproj/project.pbxproj‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@
214214
0946B05922F711AC0043469D /* NumberOf1Between1AndN_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NumberOf1Between1AndN_Solution.h; sourceTree = "<group>"; };
215215
0947D1AB232A97DB001DABA0 /* combinationSumII.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = combinationSumII.h; sourceTree = "<group>"; };
216216
0947D1AE232AA674001DABA0 /* permutationsii.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = permutationsii.h; sourceTree = "<group>"; };
217+
094E2633233AF18200C08071 /* word_search.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = word_search.h; sourceTree = "<group>"; };
218+
094E2636233BC31600C08071 /* gray_code.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gray_code.h; sourceTree = "<group>"; };
219+
095A524923364FC800D29259 /* permutation_sequence.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = permutation_sequence.h; sourceTree = "<group>"; };
220+
095A524C23371ADA00D29259 /* combinations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = combinations.h; sourceTree = "<group>"; };
221+
095A524F2339193400D29259 /* subsets.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = subsets.h; sourceTree = "<group>"; };
217222
095F60122322B3390072CF0C /* searchInRotatedSortedArrayII.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = searchInRotatedSortedArrayII.h; sourceTree = "<group>"; };
218223
0965B3CB23045219009A153E /* lengthOfLastWord.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lengthOfLastWord.h; sourceTree = "<group>"; };
219224
0965B3CE23045B1C009A153E /* addBinary.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = addBinary.h; sourceTree = "<group>"; };
@@ -224,6 +229,8 @@
224229
0969A72222E6081500CA9347 /* matching.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = matching.h; sourceTree = "<group>"; };
225230
096E161723215E7300444948 /* powx.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = powx.h; sourceTree = "<group>"; };
226231
096E161A2321681C00444948 /* search_a_2d_matrix.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = search_a_2d_matrix.h; sourceTree = "<group>"; };
232+
097149B4233D10AA00379084 /* subsets_ii.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = subsets_ii.h; sourceTree = "<group>"; };
233+
097149B7233D1B5F00379084 /* restore_ip_addresses.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = restore_ip_addresses.h; sourceTree = "<group>"; };
227234
09771DA0230C44C5000F8AC3 /* 3SumClosest.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 3SumClosest.h; sourceTree = "<group>"; };
228235
09771DA4230C4CAA000F8AC3 /* letter_combinations_of_a_phone_number.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = letter_combinations_of_a_phone_number.h; sourceTree = "<group>"; };
229236
098F84CC23234824001219CB /* find_minimum_in_rotated_sorted_array.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = find_minimum_in_rotated_sorted_array.h; sourceTree = "<group>"; };
@@ -673,9 +680,16 @@
673680
children = (
674681
09CA9FAD2318A7A500BE2B39 /* combinationSum.h */,
675682
0947D1AB232A97DB001DABA0 /* combinationSumII.h */,
683+
095A524C23371ADA00D29259 /* combinations.h */,
676684
09CA9FAE2318A7A500BE2B39 /* permutations.h */,
677685
0947D1AE232AA674001DABA0 /* permutationsii.h */,
686+
095A524923364FC800D29259 /* permutation_sequence.h */,
687+
095A524F2339193400D29259 /* subsets.h */,
688+
097149B4233D10AA00379084 /* subsets_ii.h */,
678689
09771DA4230C4CAA000F8AC3 /* letter_combinations_of_a_phone_number.h */,
690+
094E2633233AF18200C08071 /* word_search.h */,
691+
094E2636233BC31600C08071 /* gray_code.h */,
692+
097149B7233D1B5F00379084 /* restore_ip_addresses.h */,
679693
);
680694
path = medium;
681695
sourceTree = "<group>";
6.26 KB
Binary file not shown.

‎backtracking.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@
2525
| 40 | [组合总和II](https://leetcode-cn.com/problems/combination-sum-ii/)| [combinationSumII](./backtracking/leetcode/medium/combinationSumII.h) | <font color=orange> medium </font> ||
2626
| 46 | [全排列](https://leetcode-cn.com/problems/permutations/)| [permutations](./backtracking/leetcode/medium/permutations.h) | <font color=orange> medium </font> ||
2727
| 47 | [全排列II](https://leetcode-cn.com/problems/permutations-ii/)| [permutationsii](./backtracking/leetcode/medium/permutationsii.h) | <font color=orange> medium </font> ||
28-
28+
| 60 | [第k个排列](https://leetcode-cn.com/problems/permutation-sequence/)| [permutation_sequence](./backtracking/leetcode/medium/permutation_sequence.h) | <font color=orange> medium </font> ||
29+
| 77 | [组合](https://leetcode-cn.com/problems/combinations/)| [combinations](./backtracking/leetcode/medium/combinations.h) | <font color=orange> medium </font> ||
30+
| 78 | [子集](https://leetcode-cn.com/problems/subsets/)| [subsets](./backtracking/leetcode/medium/subsets.h) | <font color=orange> medium </font> ||
31+
| 79 | [单词搜索](https://leetcode-cn.com/problems/word-search/)| [word_search](./backtracking/leetcode/medium/word_search.h) | <font color=orange> medium </font> ||
32+
| 89 | [格雷编码](https://leetcode-cn.com/problems/gray-code/)| [gray_code](./backtracking/leetcode/medium/gray_code.h) | <font color=orange> medium </font> ||
33+
| 90 | [子集II](https://leetcode-cn.com/problems/subsets-ii)| [subsets_ii](./backtracking/leetcode/medium/subsets_ii.h) | <font color=orange> medium </font> ||
34+
| 93 | [复原IP地址](https://leetcode-cn.com/problems/restore-ip-addresses)| [restore_ip_addresses](./backtracking/leetcode/medium/restore_ip_addresses.h) | <font color=orange> medium </font> ||
2935
### 剑指Offer
3036
| &emsp;题号&emsp; | 题目链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| 答案链接&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;| &emsp;难度&emsp; | &emsp;完成度&emsp; |
3137
| :--: | :--: | :----------------------------------------------------------- | :----------------------------------------------------------- | :------: |
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// combinations.h
3+
// backtracking
4+
//
5+
// Created by junl on 2019年9月22日.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef combinations_hpp
10+
#define combinations_hpp
11+
12+
#include <stdio.h>
13+
14+
/*
15+
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
16+
17+
示例:
18+
19+
输入: n = 4, k = 2
20+
输出:
21+
[
22+
[2,4],
23+
[3,4],
24+
[2,3],
25+
[1,2],
26+
[1,3],
27+
[1,4],
28+
]
29+
30+
来源:力扣(LeetCode)
31+
链接:https://leetcode-cn.com/problems/combinations
32+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
33+
*/
34+
35+
namespace leetcode {
36+
class Solution77 {
37+
public:
38+
/*
39+
思路:回溯
40+
其实这题和全排列思路是一样的,区别在于排列数只有k个数.
41+
42+
我们把问题的求解分为K个阶段,每个阶段需要从1...n中随机选择一个数,这样K个阶段后,就能求出k个数的组合.
43+
44+
这种解法效率有点低,看解法2
45+
*/
46+
vector<vector<int>> combine(int n, int k) {
47+
if (k > n) return {};
48+
49+
vector<int> elements;
50+
vector<int> st;
51+
st.resize(n+1);
52+
vector<vector<int>> results;
53+
combine(n, k, elements, st, results);
54+
return results;
55+
}
56+
57+
void combine(int n, int k, vector<int> elements, vector<int> st, vector<vector<int>>& results){
58+
if (elements.size() == k){
59+
//find
60+
results.push_back(elements);
61+
return;
62+
}
63+
64+
for(int i=1;i<=n;i++){
65+
if(st[i]) //跳过重复数字
66+
continue;
67+
if(elements.size()>0 && i<= elements.back()){
68+
continue;
69+
}
70+
elements.push_back(i);st[i]=true;
71+
combine(n, k, elements, st, results);
72+
elements.pop_back();st[i]=false;
73+
}
74+
75+
}
76+
77+
78+
/*
79+
思路:
80+
利用模式匹配,比如n=5,k=3.构建状态[11100],我们需要将它变换成[00111],即把左边的1依次移动到右边去,每一次移动就是一种排列的可能。
81+
*/
82+
vector<vector<int>> combine2(int n, int k){
83+
if (k > n) return {};
84+
vector<vector<int>> results;
85+
vector<int> st;
86+
for (int i=0; i<n; i++) {
87+
st.push_back(i<k ? 1 : 0);
88+
}
89+
while (true) {
90+
vector<int> temp;
91+
for (int i=0; i<n; i++) {
92+
if (st[i]) {
93+
temp.push_back(i);
94+
}
95+
}
96+
results.push_back(temp);
97+
98+
//1.找到10的结构,然后转换为01
99+
//2.移动右边的1到最左边
100+
int found = false;
101+
int one_count = 0;
102+
for (int i=0; i<n-1; i++) {
103+
if (st[i] == 1 && st[i+1] == 0) {
104+
st[i]=0;st[i+1]=1;
105+
found = true;
106+
107+
for (int j=0; j<i; j++) {
108+
st[j] = one_count-- > 0 ? 1:0;
109+
}
110+
break;
111+
}
112+
if (st[i] == 1){
113+
one_count++;
114+
}
115+
}
116+
if (!found) {
117+
break;
118+
}
119+
}
120+
121+
122+
return results;
123+
}
124+
};
125+
126+
void test_combinations(){
127+
Solution77 solution;
128+
cout << "test_combinations starting ........" << endl;
129+
auto x = solution.combine(4, 2);
130+
x;
131+
}
132+
}
133+
#endif /* combinations_hpp */
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// gray_code.h
3+
// backtracking
4+
//
5+
// Created by junl on 2019年9月25日.
6+
// Copyright © 2019 junl. All rights reserved.
7+
//
8+
9+
#ifndef gray_code_hpp
10+
#define gray_code_hpp
11+
12+
#include <stdio.h>
13+
#include <math.h>
14+
/*
15+
89. 格雷编码
16+
格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异。
17+
18+
给定一个代表编码总位数的非负整数 n,打印其格雷编码序列。格雷编码序列必须以 0 开头。
19+
20+
示例 1:
21+
22+
输入: 2
23+
输出: [0,1,3,2]
24+
解释:
25+
00 - 0
26+
01 - 1
27+
11 - 3
28+
10 - 2
29+
30+
对于给定的 n,其格雷编码序列并不唯一。
31+
例如,[0,2,3,1] 也是一个有效的格雷编码序列。
32+
33+
00 - 0
34+
10 - 2
35+
11 - 3
36+
01 - 1
37+
*/
38+
39+
namespace leetcode {
40+
class Solution89 {
41+
public:
42+
vector<int> results;
43+
44+
vector<int> grayCode(int n) {
45+
if (n == 0) return {0};
46+
map<string,bool> st;
47+
string elements;
48+
for(int i=0;i<n;i++){
49+
elements+='0';
50+
}
51+
int totalCount = pow(2,n);
52+
grayCode(elements, st, totalCount);
53+
return results;
54+
}
55+
56+
void grayCode(string elements, map<string,bool> &st, int totalCount){
57+
if (results.size() == totalCount || st[elements])
58+
return;
59+
int loc = calc10(elements);
60+
results.push_back(loc);
61+
st[elements] = true;
62+
for(int i=0;i<elements.size();i++){
63+
elements[i] = elements[i] == '0' ? '1' : '0';
64+
grayCode(elements, st, totalCount);
65+
elements[i] = elements[i] == '0' ? '1' : '0';
66+
}
67+
}
68+
69+
int calc10(string &nums){
70+
int sum=0;
71+
int l = 1;
72+
for(int i=nums.size()-1;i>=0;i--){
73+
sum+=(nums[i]-'0')*l;
74+
l*=2;
75+
}
76+
return sum;
77+
}
78+
};
79+
80+
void test_gray_code(){
81+
cout << "test_gray_code starting...." << endl;
82+
Solution89 so;
83+
vector<int> results = so.grayCode(2);
84+
for (auto &i:results){
85+
cout << i << ", ";
86+
}
87+
cout << endl;
88+
}
89+
}
90+
#endif /* gray_code_hpp */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /