Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb6c4f9

Browse files
committed
feat: add solutions to lc problems: No.0039,0040,1346
- No.0039.Combination Sum - No.0040.Combination Sum II - No.1346.Check If N and Its Double Exist
1 parent ade0f78 commit cb6c4f9

File tree

11 files changed

+463
-5
lines changed

11 files changed

+463
-5
lines changed

‎solution/0000-0099/0039.Combination Sum/README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,60 @@ func combinationSum(candidates []int, target int) [][]int {
182182
}
183183
```
184184

185+
### **TypeScript**
186+
187+
```ts
188+
function combinationSum(candidates: number[], target: number): number[][] {
189+
const n = candidates.length;
190+
const t: number[] = [];
191+
const res: number[][] = [];
192+
const dfs = (i: number, sum: number) => {
193+
if (sum > target) {
194+
return;
195+
}
196+
if (sum === target) {
197+
res.push([...t]);
198+
return;
199+
}
200+
for (let j = i; j < n; j++) {
201+
t.push(candidates[j]);
202+
dfs(j, sum + candidates[j]);
203+
t.pop();
204+
}
205+
};
206+
dfs(0, 0);
207+
return res;
208+
}
209+
```
210+
211+
### **Rust**
212+
213+
```rust
214+
impl Solution {
215+
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
216+
if count < 0 {
217+
return;
218+
}
219+
if count == 0 {
220+
res.push(t.clone());
221+
return;
222+
}
223+
for j in i..candidates.len() {
224+
let num = candidates[j];
225+
t.push(num);
226+
Self::dfs(j, count - num, candidates, t, res);
227+
t.pop();
228+
}
229+
}
230+
231+
pub fn combination_count(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
232+
let mut res = Vec::new();
233+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
234+
res
235+
}
236+
}
237+
```
238+
185239
### **...**
186240

187241
```

‎solution/0000-0099/0039.Combination Sum/README_EN.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,60 @@ func combinationSum(candidates []int, target int) [][]int {
172172
}
173173
```
174174

175+
### **TypeScript**
176+
177+
```ts
178+
function combinationSum(candidates: number[], target: number): number[][] {
179+
const n = candidates.length;
180+
const t: number[] = [];
181+
const res: number[][] = [];
182+
const dfs = (i: number, sum: number) => {
183+
if (sum > target) {
184+
return;
185+
}
186+
if (sum === target) {
187+
res.push([...t]);
188+
return;
189+
}
190+
for (let j = i; j < n; j++) {
191+
t.push(candidates[j]);
192+
dfs(j, sum + candidates[j]);
193+
t.pop();
194+
}
195+
};
196+
dfs(0, 0);
197+
return res;
198+
}
199+
```
200+
201+
### **Rust**
202+
203+
```rust
204+
impl Solution {
205+
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
206+
if count < 0 {
207+
return;
208+
}
209+
if count == 0 {
210+
res.push(t.clone());
211+
return;
212+
}
213+
for j in i..candidates.len() {
214+
let num = candidates[j];
215+
t.push(num);
216+
Self::dfs(j, count - num, candidates, t, res);
217+
t.pop();
218+
}
219+
}
220+
221+
pub fn combination_count(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
222+
let mut res = Vec::new();
223+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
224+
res
225+
}
226+
}
227+
```
228+
175229
### **...**
176230

177231
```
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: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3+
if count < 0 {
4+
return;
5+
}
6+
if count == 0 {
7+
res.push(t.clone());
8+
return;
9+
}
10+
for j in i..candidates.len() {
11+
let num = candidates[j];
12+
t.push(num);
13+
Self::dfs(j, count - num, candidates, t, res);
14+
t.pop();
15+
}
16+
}
17+
18+
pub fn combination_count(candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
19+
let mut res = Vec::new();
20+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
21+
res
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const n = candidates.length;
3+
const t: number[] = [];
4+
const res: number[][] = [];
5+
const dfs = (i: number, sum: number) => {
6+
if (sum > target) {
7+
return;
8+
}
9+
if (sum === target) {
10+
res.push([...t]);
11+
return;
12+
}
13+
for (let j = i; j < n; j++) {
14+
t.push(candidates[j]);
15+
dfs(j, sum + candidates[j]);
16+
t.pop();
17+
}
18+
};
19+
dfs(0, 0);
20+
return res;
21+
}

‎solution/0000-0099/0040.Combination Sum II/README.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,69 @@ func combinationSum2(candidates []int, target int) [][]int {
196196
}
197197
```
198198

199+
### **TypeScript**
200+
201+
```ts
202+
function combinationSum2(candidates: number[], target: number): number[][] {
203+
candidates.sort((a, b) => a - b);
204+
const n = candidates.length;
205+
const t: number[] = [];
206+
const res: number[][] = [];
207+
const dfs = (i: number, sum: number) => {
208+
if (sum > target) {
209+
return;
210+
}
211+
if (sum === target) {
212+
res.push([...t]);
213+
return;
214+
}
215+
for (let j = i; j < n; j++) {
216+
const num = candidates[j];
217+
if (j > i && num === candidates[j - 1]) {
218+
continue;
219+
}
220+
t.push(num);
221+
dfs(j + 1, sum + num);
222+
t.pop();
223+
}
224+
};
225+
dfs(0, 0);
226+
return res;
227+
}
228+
```
229+
230+
### **Rust**
231+
232+
```rust
233+
impl Solution {
234+
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
235+
if count < 0 {
236+
return;
237+
}
238+
if count == 0 {
239+
res.push(t.clone());
240+
return;
241+
}
242+
for j in i..candidates.len() {
243+
if j > i && candidates[j] == candidates[j - 1] {
244+
continue;
245+
}
246+
let num = candidates[j];
247+
t.push(num);
248+
Self::dfs(j + 1, count - num, candidates, t, res);
249+
t.pop();
250+
}
251+
}
252+
253+
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
254+
candidates.sort();
255+
let mut res = Vec::new();
256+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
257+
res
258+
}
259+
}
260+
```
261+
199262
### **...**
200263

201264
```

‎solution/0000-0099/0040.Combination Sum II/README_EN.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,69 @@ func combinationSum2(candidates []int, target int) [][]int {
181181
}
182182
```
183183

184+
### **TypeScript**
185+
186+
```ts
187+
function combinationSum2(candidates: number[], target: number): number[][] {
188+
candidates.sort((a, b) => a - b);
189+
const n = candidates.length;
190+
const t: number[] = [];
191+
const res: number[][] = [];
192+
const dfs = (i: number, sum: number) => {
193+
if (sum > target) {
194+
return;
195+
}
196+
if (sum === target) {
197+
res.push([...t]);
198+
return;
199+
}
200+
for (let j = i; j < n; j++) {
201+
const num = candidates[j];
202+
if (j > i && num === candidates[j - 1]) {
203+
continue;
204+
}
205+
t.push(num);
206+
dfs(j + 1, sum + num);
207+
t.pop();
208+
}
209+
};
210+
dfs(0, 0);
211+
return res;
212+
}
213+
```
214+
215+
### **Rust**
216+
217+
```rust
218+
impl Solution {
219+
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
220+
if count < 0 {
221+
return;
222+
}
223+
if count == 0 {
224+
res.push(t.clone());
225+
return;
226+
}
227+
for j in i..candidates.len() {
228+
if j > i && candidates[j] == candidates[j - 1] {
229+
continue;
230+
}
231+
let num = candidates[j];
232+
t.push(num);
233+
Self::dfs(j + 1, count - num, candidates, t, res);
234+
t.pop();
235+
}
236+
}
237+
238+
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
239+
candidates.sort();
240+
let mut res = Vec::new();
241+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
242+
res
243+
}
244+
}
245+
```
246+
184247
### **...**
185248

186249
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
fn dfs(i: usize, count: i32, candidates: &Vec<i32>, t: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3+
if count < 0 {
4+
return;
5+
}
6+
if count == 0 {
7+
res.push(t.clone());
8+
return;
9+
}
10+
for j in i..candidates.len() {
11+
if j > i && candidates[j] == candidates[j - 1] {
12+
continue;
13+
}
14+
let num = candidates[j];
15+
t.push(num);
16+
Self::dfs(j + 1, count - num, candidates, t, res);
17+
t.pop();
18+
}
19+
}
20+
21+
pub fn combination_sum2(mut candidates: Vec<i32>, target: i32) -> Vec<Vec<i32>> {
22+
candidates.sort();
23+
let mut res = Vec::new();
24+
Self::dfs(0, target, &candidates, &mut vec![], &mut res);
25+
res
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function combinationSum2(candidates: number[], target: number): number[][] {
2+
candidates.sort((a, b) => a - b);
3+
const n = candidates.length;
4+
const t: number[] = [];
5+
const res: number[][] = [];
6+
const dfs = (i: number, sum: number) => {
7+
if (sum > target) {
8+
return;
9+
}
10+
if (sum === target) {
11+
res.push([...t]);
12+
return;
13+
}
14+
for (let j = i; j < n; j++) {
15+
const num = candidates[j];
16+
if (j > i && num === candidates[j - 1]) {
17+
continue;
18+
}
19+
t.push(num);
20+
dfs(j + 1, sum + num);
21+
t.pop();
22+
}
23+
};
24+
dfs(0, 0);
25+
return res;
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /