Skip to content

Commit cef2b3d

Browse files
committed
update
1 parent 7333f17 commit cef2b3d

File tree

3 files changed

+96
-20
lines changed

3 files changed

+96
-20
lines changed

CATEGORY.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,47 @@ https://garciparedes.me/#headers
7676
- [ ] 264
7777
- [ ] 279
7878
- [ ] 300
79-
- [ ] 303
8079
- [ ] 309
8180
- [ ] 322
8281
- [ ] 338
8382
- [ ] 343
8483
- [ ] 357
8584
- [ ] 376
85+
86+
- 数组
87+
88+
- [x] 4
89+
- [x] 11
90+
- [ ] 15
91+
- [x] 16
92+
- [ ] 18
93+
- [x] 26
94+
- [x] 27
95+
- [x] 34
96+
- [x] 35
97+
- [ ] 39
98+
- [ ] 40
99+
- [ ] 42
100+
- [ ] 45
101+
- [ ] 48
102+
- [ ] 53
103+
- [ ] 55
104+
- [ ] 56
105+
- [ ] 59
106+
- [ ] 62
107+
- [ ] 64
108+
- [ ] 66
109+
- [ ] 73
110+
- [ ] 75
111+
- [ ] 78
112+
- [ ] 80
113+
- [ ] 88
114+
- [ ] 90
115+
- [ ] 118
116+
- [ ] 119
117+
- [ ] 120
118+
- [ ] 121
119+
- [ ] 122
120+
- [ ] 152
121+
- [ ] 153
122+
- [ ] 154

src/39-combination-sum/index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
// HELP:
2-
// backtracking
3-
4-
export const combinationSum = (candidates, target) => {
5-
let res = [];
6-
backtracking(res, [], candidates, target, 0);
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function (candidates, target) {
7+
const res = [];
8+
dfs(target, [], 0);
79
return res;
8-
};
910

10-
function backtracking(res, temp, candidates, target, start) {
11-
if (target === 0) res.push(temp.slice());
12-
if (target > 0) {
13-
for (let i = start; i < candidates.length; i++) {
14-
backtracking(
15-
res,
16-
[...temp, candidates[i]],
17-
candidates,
18-
target - candidates[i],
19-
i,
20-
);
11+
function dfs(target, combine, index) {
12+
if (index === candidates.length) {
13+
return;
14+
}
15+
if (target === 0) {
16+
res.push(combine);
17+
return;
18+
}
19+
dfs(target, combine, index + 1);
20+
if (target - candidates[index] >= 0) {
21+
dfs(target - candidates[index], [...combine, candidates[index]], index);
2122
}
2223
}
23-
}
24+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number}
5+
*/
6+
var findMedianSortedArrays = function (nums1, nums2) {
7+
const len = nums1.length + nums2.length;
8+
if (len === 1) {
9+
return ~~nums1[0] + ~~nums2[0];
10+
}
11+
const start = Math.floor(len / 2);
12+
let count = 0;
13+
const res = [];
14+
15+
while (nums1.length > 0 || nums2.length > 0) {
16+
let temp;
17+
if (nums1.length > 0 && nums2.length > 0) {
18+
if (nums1[0] < nums2[0]) {
19+
temp = nums1.shift();
20+
} else {
21+
temp = nums2.shift();
22+
}
23+
} else {
24+
temp = nums1.shift();
25+
temp = temp !== undefined ? temp : nums2.shift();
26+
}
27+
28+
if (count === start - 1 || count === start) {
29+
res.push(temp);
30+
}
31+
32+
if (res.length === 2) {
33+
return len % 2 === 1 ? res[1] : (res[0] + res[1]) / 2;
34+
}
35+
36+
count++;
37+
}
38+
};

0 commit comments

Comments
 (0)