ID | Title | Difficulty | |
---|---|---|---|
Loading... |
77. Combinations
Medium
LeetCode
Backtracking
Problem
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
Code
class Solution {
public List<List<Integer>> combine(int n, int k) {
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = i + 1;
}
List<List<Integer>> res = new ArrayList<>();
helper(res, nums, new ArrayList<>(), 0, k);
return res;
}
public void helper(List<List<Integer>> res, int[] nums, List<Integer> temp, int index, int count){
if(count == 0){
res.add(new ArrayList<>(temp));
return;
}
for(int i = index; i < nums.length; i++){
temp.add(nums[i]);
helper(res, nums, temp, i + 1, count - 1);
temp.remove(temp.size() - 1);
}
}
}
按 <- 键看上一题!
76. Minimum Window Substring
按 -> 键看下一题!
78. Subsets