Topic
- Backtracking
Description
https://leetcode.com/problems/combinations/
Given two integers n
and k
, return all possible combinations of k
numbers out of 1 ... n
.
You may return the answer in any order.
Example 1:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
Example 2:
Input: n = 1, k = 1
Output: [[1]]
Constraints:
- 1 <=
n
<= 20 - 1 <=
k
<= n
Analysis
Submission
import java.util.ArrayList;
import java.util.List;
public class Combinations {
public List<List<Integer>> combine1(int numRange, int expectedSize) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backtracking(path, numRange, expectedSize, 1, result);
return result;
}
private void backtracking(List<Integer> path, int numRange, int expectedSize, int startIndex,
List<List<Integer>> result) {
if (path.size() == expectedSize) {
result.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= numRange; i++) {
path.add(i);
backtracking(path, numRange, expectedSize, i + 1, result);
path.remove(path.size() - 1);
}
}
// 剪枝优化后的
public List<List<Integer>> combine2(int numRange, int expectedSize) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
backtracking2(path, numRange, expectedSize, 1, result);
return result;
}
private void backtracking2(List<Integer> path, int numRange, int expectedSize, int startIndex,
List<List<Integer>> result) {
if (path.size() == expectedSize) {
result.add(new ArrayList<>(path));
return;
}
for (int i = startIndex; i <= numRange - (expectedSize - path.size()) + 1; i++) {
path.add(i);
backtracking(path, numRange, expectedSize, i + 1, result);
path.remove(path.size() - 1);
}
}
}
Test
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Test;
public class CombinationsTest {
@Test
@SuppressWarnings("unchecked")
public void test1() {
Combinations obj = new Combinations();
Object[] expected = {Arrays.asList(2,4),
Arrays.asList(3,4),
Arrays.asList(2,3),
Arrays.asList(1,2),
Arrays.asList(1,3),
Arrays.asList(1,4)};
assertThat(obj.combine1(4, 2), containsInAnyOrder(expected));
assertThat(obj.combine1(1, 1), containsInAnyOrder(Arrays.asList(1)));
assertThat(obj.combine1(4, 4), containsInAnyOrder(Arrays.asList(1, 2, 3, 4)));
assertThat(obj.combine1(4, 3), containsInAnyOrder(Arrays.asList(1, 2, 3), Arrays.asList(1, 3, 4),//
Arrays.asList(1, 2, 4), Arrays.asList(2, 3, 4)));
}
@Test
@SuppressWarnings("unchecked")
public void test2() {
Combinations obj = new Combinations();
Object[] expected = {Arrays.asList(2,4),
Arrays.asList(3,4),
Arrays.asList(2,3),
Arrays.asList(1,2),
Arrays.asList(1,3),
Arrays.asList(1, 4) };
assertThat(obj.combine2(4, 2), containsInAnyOrder(expected));
assertThat(obj.combine2(1, 1), containsInAnyOrder(Arrays.asList(1)));
assertThat(obj.combine2(4, 4), containsInAnyOrder(Arrays.asList(1, 2, 3, 4)));
assertThat(obj.combine2(4, 3), containsInAnyOrder(Arrays.asList(1, 2, 3), Arrays.asList(1, 3, 4), //
Arrays.asList(1, 2, 4), Arrays.asList(2, 3, 4)));
}
}
来源:oschina
链接:https://my.oschina.net/jallenkwong/blog/4889074