Skip to content

Commit 4b9bc79

Browse files
committed
0077. Combinations
1 parent 04f1fdc commit 4b9bc79

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

markdown/0077. Combinations.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
### [77\. Combinations](https://leetcode.com/problems/combinations/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given two integers _n_ and _k_, return all possible combinations of _k_ numbers out of 1 ... _n_.
7+
8+
**Example:**
9+
10+
```
11+
Input: n = 4, k = 2
12+
Output:
13+
[
14+
[2,4],
15+
[3,4],
16+
[2,3],
17+
[1,2],
18+
[1,3],
19+
[1,4],
20+
]
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   public List<List<Integer>> combine(int n, int k) {
31+
       List<List<Integer>> result = new ArrayList<List<Integer>>();
32+
       combine(result, new ArrayList<Integer>(), 1, n, k);
33+
       return result;
34+
  }
35+
36+
   public void combine(List<List<Integer>> result, List<Integer> item, int start, int n, int k) {
37+
       if (k == 0) {
38+
           result.add(new ArrayList<Integer>(item));
39+
           return;
40+
      }
41+
       for (int i = start; i <= n; i++) {
42+
           item.add(i);
43+
           combine(result, item, i + 1, n, k - 1);
44+
           item.remove(item.size() - 1);
45+
      }
46+
  }
47+
}
48+
```
49+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190724214825.png)

src/main/java/leetcode/_77_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._77_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.combine(4, 2));
10+
}
11+
}
12+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode._77_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<List<Integer>> combine(int n, int k) {
8+
List<List<Integer>> result = new ArrayList<List<Integer>>();
9+
combine(result, new ArrayList<Integer>(), 1, n, k);
10+
return result;
11+
}
12+
13+
public void combine(List<List<Integer>> result, List<Integer> item, int start, int n, int k) {
14+
if (k == 0) {
15+
result.add(new ArrayList<Integer>(item));
16+
return;
17+
}
18+
for (int i = start; i <= n; i++) {
19+
item.add(i);
20+
combine(result, item, i + 1, n, k - 1);
21+
item.remove(item.size() - 1);
22+
}
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
### [77\. Combinations](https://leetcode.com/problems/combinations/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given two integers _n_ and _k_, return all possible combinations of _k_ numbers out of 1 ... _n_.
7+
8+
**Example:**
9+
10+
```
11+
Input: n = 4, k = 2
12+
Output:
13+
[
14+
[2,4],
15+
[3,4],
16+
[2,3],
17+
[1,2],
18+
[1,3],
19+
[1,4],
20+
]
21+
```
22+
23+
24+
#### Solution
25+
26+
Language: **Java**
27+
28+
```java
29+
class Solution {
30+
   public List<List<Integer>> combine(int n, int k) {
31+
       List<List<Integer>> result = new ArrayList<List<Integer>>();
32+
       combine(result, new ArrayList<Integer>(), 1, n, k);
33+
       return result;
34+
  }
35+
36+
   public void combine(List<List<Integer>> result, List<Integer> item, int start, int n, int k) {
37+
       if (k == 0) {
38+
           result.add(new ArrayList<Integer>(item));
39+
           return;
40+
      }
41+
       for (int i = start; i <= n; i++) {
42+
           item.add(i);
43+
           combine(result, item, i + 1, n, k - 1);
44+
           item.remove(item.size() - 1);
45+
      }
46+
  }
47+
}
48+
```
49+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190724214825.png)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy