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],
]

SLOUTION 1:

一段时间不写真心容易不记得。

这是一道典型的模板题。以下是模板写法。

1. 必须时刻注意,for循环里 是i+1不是Index+1,这个老是会忘记。我们当前取过后,应该是处理下一个位置。

2. start 应该是从1开始。这个是这题的特别情况,一般idnex是0开始。

3. combination的题目,注意因为没有顺序,所以为了避免重复的一些解,我们只考虑递增的解。下一个排列题就不一样了。

 1 // 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
 2     // 的组合。
 3     public void dfs(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
 4         if (path.size() == k) {
 5             ret.add(new ArrayList<Integer>(path));
 6             return;
 7         }
 8
 9         // 注意这里的条件i <= n 取n也是合法的!
10         // Example:
11         for (int i = start; i <= n; i++) {
12             path.add(i);
13
14             // 注意,最后一个参数是i + 1,不是start + 1!!
15             dfs(n, k, path, ret, i + 1);
16             path.remove(path.size() - 1);
17         }
18     }

SLOUTION 2:

为了优化,在for 中加了一个判断,及时终止。i <= (n - k + 1)

 1 // 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
 2     // 的组合。
 3     public void dfs2(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
 4         if (0 == k) {
 5             ret.add(new ArrayList<Integer>(path));
 6             return;
 7         }
 8
 9         // 注意这里的条件i <= n 取n也是合法的!
10         // Example:
11         for (int i = start; i <= (n - k + 1); i++) {
12             path.add(i);
13
14             // 注意,最后一个参数是i + 1,不是start + 1!!
15             dfs(n, k - 1, path, ret, i + 1);
16             path.remove(path.size() - 1);
17         }
18     }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/Combine_1203.java

时间: 2024-10-31 13:31:21

LeetCode: Combinations 解题报告的相关文章

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

【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], ] [解析] 题意

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

LeetCode: Subsets 解题报告

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

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/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 i

Two Sum | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/two-sum/ 题目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe

LeetCode: isSameTree1 解题报告

isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. SOLUTION 1 & SOLUTION 2: 以下是递归及非递归解法: 1. 递归解法就是判断当前节点是