Skip to content

Commit 63d57b9

Browse files
authored
77 solved. (#39)
1 parent 05f8ec3 commit 63d57b9

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ continually updating 😃.
7878
* [435. Non-overlapping Intervals](./src/0435_non_overlapping_intervals/greedy_solution.go)   *`greedy;`*  *`dynamic programming`*
7979
* [455. Assign Cookies](./src/0455_assign_cookies/assign_cookies.go)
8080

81+
### Backtracking
82+
* [77. Combinations](src/0077_combinations/combinations.go)   *`combine`*
83+
8184
### Binary Tree
8285
* [94. Binary Tree Inorder Traversal](./src/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)   *`stack`*
8386
* [100. Same Tree](./src/0100_same_tree/same_tree.go)   *`dfs`*
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
77. Combinations
3+
https://leetcode.com/problems/combinations/
4+
5+
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
6+
*/
7+
// time: 2019-01-04
8+
9+
package combinations
10+
11+
// backtracking
12+
// time complexity: O(n^k)
13+
// space complexity: O(k)
14+
func combine(n int, k int) [][]int {
15+
if n <= 0 || k <= 0 || k > n {
16+
return [][]int{}
17+
}
18+
res := make([][]int, 0)
19+
c := make([]int, 0, k)
20+
generateCombinations(n, k, 1, c, &res)
21+
return res
22+
}
23+
24+
// 求解C(n,k), 当前已经找到的组合存储在c中,需要从start开始搜索新元素。
25+
func generateCombinations(n, k, start int, c []int, res *[][]int) {
26+
if len(c) == k {
27+
cpy := make([]int, k)
28+
copy(cpy, c)
29+
*res = append(*res, cpy)
30+
return
31+
}
32+
// 回朔法剪枝。
33+
// 还有k - len(c)个空位,所以, [i...n]中至少要有k - len(c)个元素
34+
// i最多为n-(k-len(c))+1
35+
for i := start; i <= n-(k-len(c))+1; i++ {
36+
c = append(c, i)
37+
generateCombinations(n, k, i+1, c, res)
38+
c = c[:len(c)-1]
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package combinations
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestCombine(t *testing.T) {
9+
type arg struct {
10+
n, k int
11+
}
12+
13+
testCases := []arg{
14+
{n: 0, k: 2},
15+
{n: 2, k: 0},
16+
{n: 1, k: 5},
17+
{n: 4, k: 2},
18+
}
19+
expected := [][][]int{
20+
{},
21+
{},
22+
{},
23+
{{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}},
24+
}
25+
26+
for index, data := range testCases {
27+
if res := combine(data.n, data.k); !reflect.DeepEqual(res, expected[index]) {
28+
t.Errorf("expected %v, got %v", expected[index], res)
29+
}
30+
}
31+
}

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
3838
|0075|[75. Sort Colors](0075_sort_colors/sort_colors.go)|Medium|*`sort`*|
3939
|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|
40+
|0077|[77. Combinations](0077_combinations/combinations.go)|Medium|*`backtracking;`**`combine`*|
4041
|0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*|
4142
|0088|[88. Merge Sorted Array](0088_merge_sorted_array/msa.go)|Easy|*`sort`*|
4243
|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|

0 commit comments

Comments
 (0)