LeetCode

安稳与你 提交于 2021-01-11 01:39:01

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)));
	}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!