【Leetcode】Combinations (Backtracking)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,

If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

这是一道经典的NP问题,采用回朔法

i从1开始,

先加入1,再加入2 temp=[1,2]

然后去掉2 加3 temp=[1,3]

然后去掉3 加4 temp=[1,4]

然后去掉4 i=5不合法了 结束for循环

然后去掉1

返回到i=1,然后i++

i现在是2了

先加入2,再加入3 temp=[2,3]

然后去掉3 加4 temp=[2,4]

然后去掉4 i=5不合法了 结束for循环

然后去掉2

返回到i=2,然后i++

i现在是3了

先加入3 再加入4 temp=[3,4]

然后去掉4 i=5不合法了 结束for循环

然后去掉3

返回到i=3,然后i++

i现在是4了 先加入4 i=5不合法了 结束for循环

然后去掉4

返回到4,然后i++

i现在是5了 结束for循环

全部结束

	public ArrayList<ArrayList<Integer>> combine(int n, int k) {
		ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> temp = new ArrayList<Integer>();
		if (n <= 0 || k < 0)
			return result;
		helper(n, k, 1, temp, result);
		return result;
	}

	public void helper(int n, int k, int startNum, ArrayList<Integer> temp,
			ArrayList<ArrayList<Integer>> result) {
		if (k == temp.size()) {
			result.add(new ArrayList<Integer>(temp));
			return;
		}
		for (int i = startNum; i <= n; i++) {
			temp.add(i);
			helper(n, k, i + 1, temp, result);
			temp.remove(temp.size() - 1);
		}
	}

至于为什么要temp.remove?因为要保护现场,要知道return语句只能恢复i以前的值,却不能改变temp里面的值。

时间: 2024-10-06 07:07:14

【Leetcode】Combinations (Backtracking)的相关文章

【LeetCode】Combinations 解题报告

好像自从上次 CSDN Blog 系统故障后,博文阅读量过百后系统不再发放C币,尽管我也不知道C币有啥用. [题目] Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] [解析] 题意

【leetcode】Combinations (middle)

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 思路:有点像0-1背包问题, 对于从1-n的每一个数字都可以选择放入答案 和不放入答案. 当长度达到k时就是一个符合条件的解. 递归的

【LeetCode】Combinations

Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 递推点:加入i后,下一个加入的元素需要遍历i+1~n 因此可以基于k做递归. base case: k=1,

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->