LeetCode 77. 组合(Combinations)

题目描述

给定两个整数 n 和 k,返回 1 ... 中所有可能的 k 个数的组合。

示例:

输入: n = 4, k = 2
输出:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

解题思路

回溯法,每次遍历到一个元素分为放入与不放入集合两种情况,若集合长度为k,则加入到结果中。

代码

 1 class Solution {
 2 public:
 3     vector<vector<int>> combine(int n, int k) {
 4         vector<vector<int>> res;
 5         vector<int> nums, temp;
 6         for(int i = 0; i < n; i++)
 7             nums.push_back(i + 1);
 8         cmb(n, k, 0, nums, temp, res);
 9         return res;
10     }
11     void cmb(int n, int k, int idx, vector<int> nums, vector<int> &temp, vector<vector<int>> &res){
12         if(k && temp.size() == k)
13             res.push_back(temp);
14         else if(idx < n){
15             temp.push_back(nums[idx]);
16             cmb(n, k, idx + 1, nums, temp, res);
17             temp.pop_back();
18             cmb(n, k, idx + 1, nums, temp, res);
19         }
20     }
21 };

原文地址:https://www.cnblogs.com/wmx24/p/9547360.html

时间: 2024-11-14 12:42:06

LeetCode 77. 组合(Combinations)的相关文章

leetcode || 77、Combinations

problem: 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], ] Hide Tags Backtracking 题意:输出1~n的 k 个数字的所有排列组合 thinking: (

[Lintcode]152. Combinations/[Leetcode]77. Combinations

152. Combinations/77. Combinations 本题难度: Medium Topic: Search & Recursion Description Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example Given n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2]

LeetCode:组合总数II【40】

LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. 说明: 所有数字(包括目标数)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8, 所求解集为: [ [1, 7], [1, 2, 5], [2, 6],

leetCode 77.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], ] 思路:此题意思是给一个n,和k,求1-n的k个数的组合.没有反复(位置交换算反复).可用排列组合的统一公式求解. 代码例如以下: p

leetcode[77] Combinations

给定n和k,从1到n中选k个数,存到结果中.其实就是组合问题.例如 If n = 3, k = 2, 结果是 { 1,2], [1,3], [2,3] }; 思路:利用回溯法. class Solution { public: void dfs77(vector<vector<int > > &ans, vector<int> subans, int start, int n, int k) { if (subans.size() == k) { ans.pus

LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap<Character, String> map = new HashMap<>(); map.put('0', "0"); map.put('1', "1"); map.put('2', "abc"); map.put('3', &

leetcode 77 Combinations ----- java

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], ] 求C(k,n)的所有结果. 利用回溯(backtracking)来求解释道题是比较简单且效率较高的. public class Sol

77. 组合

给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2输出:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/combinations著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 1 public class Solution { 2 private boolean[

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Outpu