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 Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        Con(n,k,0,0,res,new int[k]);
        return res;

    }
    public void Con( int n,int k,int num,int start,List<List<Integer>> res,int[] ans){
        if( num == k){
            ArrayList<Integer> tt = new ArrayList<Integer>();
            for( int i = 0;i<k;i++)
                tt.add(ans[i]);
            res.add(tt);
            return ;
        }
        for( int i = start;i<n;i++){
            ans[num] = i+1;
            Con(n,k,num+1,i+1,res,ans);
        }
        return ;
}
}

有一点可以修正,就是Con中的for循环,改为

for( int i = start;i<=n-(k-num);i++)

这样可以进一步提高速度。还有就是把res设为全局变量会代码好看一点。。

时间: 2024-12-15 06:56:00

leetcode 77 Combinations ----- java的相关文章

[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 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: Letter Combinations of a Phone Number [018]

[题目] 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" Output: ["ad", "ae"

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" Outp

LeetCode: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], ]DFS,递归 1 class Solution { 2 public: 3 vector<vector<int> > an

Leetcode: Wildcard Matching. java

Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. '*' Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function p

Leetcode: Text Justification. java

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad ex

Java for LeetCode 077 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], ] 解题思路: DFS,JAVA实现如下: public static void main(String args[]) { List<