-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
32 lines (30 loc) · 781 Bytes
/
Copy pathsolution.java
File metadata and controls
32 lines (30 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 77. Combinations
// https://leetcode.com/problems/combinations/
// Medium | Java | Accepted 2026-08-22
// Runtime 14 ms | Memory 97.9 MB
class Solution {
List<List<Integer>> ans = new ArrayList<>();
int kk;
int nn;
public List<List<Integer>> combine(int n, int k) {
nn = n;
kk = k;
List<Integer> temp = new ArrayList<>();
backtrack(temp, 1);
return ans;
}
public void backtrack(List<Integer> temp, int start)
{
if(temp.size()==kk)
{
ans.add(new ArrayList<>(temp));
return;
}
for(int i = start; i<=nn-(kk-temp.size())+1; i++)
{
temp.add(i);
backtrack(temp, i+1);
temp.remove(temp.size()-1);
}
}
}