Skip to content

Commit 8dd5cb6

Browse files
committed
feat: add solutions to lc problem: No.0046,0077
- No.0046.Permutations - No.0077.Combinations
1 parent 34a7cd8 commit 8dd5cb6

File tree

8 files changed

+274
-0
lines changed

8 files changed

+274
-0
lines changed

solution/0000-0099/0046.Permutations/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,53 @@ func dfs(u, n int, nums []int, used []bool, path []int, res *[][]int) {
186186
}
187187
```
188188

189+
### **TypeScript**
190+
191+
```ts
192+
function permute(nums: number[]): number[][] {
193+
const res: number[][] = [];
194+
const dfs = (paths: number[]) => {
195+
if (paths.length === nums.length) {
196+
res.push(paths);
197+
return;
198+
}
199+
for (const num of nums) {
200+
if (!paths.includes(num)) {
201+
dfs(paths.concat(num));
202+
}
203+
}
204+
};
205+
dfs([]);
206+
return res;
207+
}
208+
```
209+
210+
### **Rust**
211+
212+
```rust
213+
impl Solution {
214+
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
215+
if paths.len() == nums.len() {
216+
res.push(paths.clone());
217+
return;
218+
}
219+
for i in nums.iter() {
220+
if !paths.contains(i) {
221+
paths.push(*i);
222+
Self::dfs(nums, paths, res);
223+
paths.pop();
224+
}
225+
}
226+
}
227+
228+
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
229+
let mut res = vec![];
230+
Self::dfs(&nums, &mut vec![], &mut res);
231+
res
232+
}
233+
}
234+
```
235+
189236
### **...**
190237

191238
```

solution/0000-0099/0046.Permutations/README_EN.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,53 @@ func dfs(u, n int, nums []int, used []bool, path []int, res *[][]int) {
185185
}
186186
```
187187

188+
### **TypeScript**
189+
190+
```ts
191+
function permute(nums: number[]): number[][] {
192+
const res: number[][] = [];
193+
const dfs = (paths: number[]) => {
194+
if (paths.length === nums.length) {
195+
res.push(paths);
196+
return;
197+
}
198+
for (const num of nums) {
199+
if (!paths.includes(num)) {
200+
dfs(paths.concat(num));
201+
}
202+
}
203+
};
204+
dfs([]);
205+
return res;
206+
}
207+
```
208+
209+
### **Rust**
210+
211+
```rust
212+
impl Solution {
213+
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
214+
if paths.len() == nums.len() {
215+
res.push(paths.clone());
216+
return;
217+
}
218+
for i in nums.iter() {
219+
if !paths.contains(i) {
220+
paths.push(*i);
221+
Self::dfs(nums, paths, res);
222+
paths.pop();
223+
}
224+
}
225+
}
226+
227+
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
228+
let mut res = vec![];
229+
Self::dfs(&nums, &mut vec![], &mut res);
230+
res
231+
}
232+
}
233+
```
234+
188235
### **...**
189236

190237
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3+
if paths.len() == nums.len() {
4+
res.push(paths.clone());
5+
return;
6+
}
7+
for i in nums.iter() {
8+
if !paths.contains(i) {
9+
paths.push(*i);
10+
Self::dfs(nums, paths, res);
11+
paths.pop();
12+
}
13+
}
14+
}
15+
16+
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
17+
let mut res = vec![];
18+
Self::dfs(&nums, &mut vec![], &mut res);
19+
res
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function permute(nums: number[]): number[][] {
2+
const res: number[][] = [];
3+
const dfs = (paths: number[]) => {
4+
if (paths.length === nums.length) {
5+
res.push(paths);
6+
return;
7+
}
8+
for (const num of nums) {
9+
if (!paths.includes(num)) {
10+
dfs(paths.concat(num));
11+
}
12+
}
13+
};
14+
dfs([]);
15+
return res;
16+
}

solution/0000-0099/0077.Combinations/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,57 @@ func dfs(i, n, k int, t []int, res *[][]int) {
130130
}
131131
```
132132

133+
### **TypeScript**
134+
135+
```ts
136+
function combine(n: number, k: number): number[][] {
137+
const res: number[][] = [];
138+
const dfs = (i: number, t: number[]) => {
139+
if (t.length == k) {
140+
res.push(t);
141+
return;
142+
}
143+
// 剪枝
144+
if (t.length + n - i + 1 < k) {
145+
return;
146+
}
147+
for (let j = i; j <= n; j++) {
148+
dfs(j + 1, [...t, j]);
149+
}
150+
};
151+
dfs(1, []);
152+
return res;
153+
}
154+
```
155+
156+
### **Rust**
157+
158+
```rust
159+
impl Solution {
160+
fn dfs(i: i32, n: i32, k: i32, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
161+
if k == 0 {
162+
res.push(t.clone());
163+
return;
164+
}
165+
// 剪枝
166+
if n - i + 1 < k {
167+
return;
168+
}
169+
for j in i..=n {
170+
t.push(j);
171+
Self::dfs(j + 1, n, k - 1, t, res);
172+
t.pop();
173+
}
174+
}
175+
176+
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
177+
let mut res = vec![];
178+
Self::dfs(1, n, k, &mut vec![], &mut res);
179+
res
180+
}
181+
}
182+
```
183+
133184
### **...**
134185

135186
```

solution/0000-0099/0077.Combinations/README_EN.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,57 @@ func dfs(i, n, k int, t []int, res *[][]int) {
142142
}
143143
```
144144

145+
### **TypeScript**
146+
147+
```ts
148+
function combine(n: number, k: number): number[][] {
149+
const res: number[][] = [];
150+
const dfs = (i: number, t: number[]) => {
151+
if (t.length == k) {
152+
res.push(t);
153+
return;
154+
}
155+
// pruning
156+
if (t.length + n - i + 1 < k) {
157+
return;
158+
}
159+
for (let j = i; j <= n; j++) {
160+
dfs(j + 1, [...t, j]);
161+
}
162+
};
163+
dfs(1, []);
164+
return res;
165+
}
166+
```
167+
168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
fn dfs(i: i32, n: i32, k: i32, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
173+
if k == 0 {
174+
res.push(t.clone());
175+
return;
176+
}
177+
// pruning
178+
if n - i + 1 < k {
179+
return;
180+
}
181+
for j in i..=n {
182+
t.push(j);
183+
Self::dfs(j + 1, n, k - 1, t, res);
184+
t.pop();
185+
}
186+
}
187+
188+
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
189+
let mut res = vec![];
190+
Self::dfs(1, n, k, &mut vec![], &mut res);
191+
res
192+
}
193+
}
194+
```
195+
145196
### **...**
146197

147198
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
fn dfs(i: i32, n: i32, k: i32, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3+
if k == 0 {
4+
res.push(t.clone());
5+
return;
6+
}
7+
// 剪枝
8+
if n - i + 1 < k {
9+
return;
10+
}
11+
for j in i..=n {
12+
t.push(j);
13+
Self::dfs(j + 1, n, k - 1, t, res);
14+
t.pop();
15+
}
16+
}
17+
18+
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
19+
let mut res = vec![];
20+
Self::dfs(1, n, k, &mut vec![], &mut res);
21+
res
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function combine(n: number, k: number): number[][] {
2+
const res: number[][] = [];
3+
const dfs = (i: number, t: number[]) => {
4+
if (t.length == k) {
5+
res.push(t);
6+
return;
7+
}
8+
// 剪枝
9+
if (t.length + n - i + 1 < k) {
10+
return;
11+
}
12+
for (let j = i; j <= n; j++) {
13+
dfs(j + 1, [...t, j]);
14+
}
15+
};
16+
dfs(1, []);
17+
return res;
18+
}

0 commit comments

Comments
 (0)