Skip to content

Commit e7d9114

Browse files
committed
finish 77
1 parent 4d2fefb commit e7d9114

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

001-100/77. Combinations.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 77. Combinations
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Backtracking.
5+
- Similar Questions: Combination Sum, Permutations.
6+
7+
## Problem
8+
9+
Given two integers **n** and **k**, return all possible combinations of **k** numbers out of 1 ... **n**.
10+
11+
**Example:**
12+
13+
```
14+
Input: n = 4, k = 2
15+
Output:
16+
[
17+
[2,4],
18+
[3,4],
19+
[2,3],
20+
[1,2],
21+
[1,3],
22+
[1,4],
23+
]
24+
```
25+
26+
## Solution
27+
28+
```javascript
29+
/**
30+
* @param {number} n
31+
* @param {number} k
32+
* @return {number[][]}
33+
*/
34+
var combine = function(n, k) {
35+
if (n < k || k < 1) return [];
36+
37+
var res = [];
38+
39+
helper(res, [], 0, n, k);
40+
41+
return res;
42+
};
43+
44+
var helper = function (res, now, start, n, k) {
45+
if (k === 0) {
46+
res.push(Array.from(now));
47+
return;
48+
}
49+
50+
for (var i = start; i < n; i++) {
51+
now.push(i + 1)
52+
helper(res, now, i + 1, n, k - 1);
53+
now.pop();
54+
}
55+
};
56+
```
57+
58+
**Explain:**
59+
60+
nope.
61+
62+
**Complexity:**
63+
64+
* Time complexity :
65+
* Space complexity :

001-100/78. Subsets.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 78. Subsets
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Backtracking, Bit Manipulation.
5+
- Similar Questions: Subsets II, Generalized Abbreviation, Letter Case Permutation.
6+
7+
## Problem
8+
9+
Given a set of **distinct** integers, **nums**, return all possible subsets (the power set).
10+
11+
**Note:** The solution set must not contain duplicate subsets.
12+
13+
**Example:**
14+
15+
```
16+
Input: nums = [1,2,3]
17+
Output:
18+
[
19+
[3],
20+
  [1],
21+
  [2],
22+
  [1,2,3],
23+
  [1,3],
24+
  [2,3],
25+
  [1,2],
26+
  []
27+
]
28+
```
29+
30+
## Solution
31+
32+
```javascript
33+
34+
```
35+
36+
**Explain:**
37+
38+
nope.
39+
40+
**Complexity:**
41+
42+
* Time complexity : O(n).
43+
* Space complexity : O(n).

0 commit comments

Comments
 (0)