Skip to content

Commit 15a2ad1

Browse files
committed
update: 39
1 parent 362a130 commit 15a2ad1

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3030
|33|[Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [JavaScript](./src/search-in-rotated-sorted-array/res.js)|Medium|
3131
|34|[Search for a Range](https://leetcode.com/problems/search-for-a-range/) | [JavaScript](./src/search-for-a-range/res.js)|Medium|
3232
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [JavaScript](./src/search-insert-position/res.js)|Easy|
33+
|39|[Combination Sum](https://leetcode.com/problems/combination-sum/) | [JavaScript](./src/combination-sum/res.js)|Medium|
3334
|41|[First Missing Positive](https://leetcode.com/problems/first-missing-positive/description/) | [JavaScript](./src/first-missing-positive/res.js)|Hard|
3435
|42|[ Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/) | [JavaScript](./src/trapping-rain-water/res.js)|Hard|
3536
|43|[Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [JavaScript](./src/multiply-strings/res.js)|Medium|

src/combination-sum/res.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function(candidates, target) {
7+
candidates.sort((a, b) => b-a);
8+
const res = [];
9+
10+
const calCombs = (candis, resArr, targ) => {
11+
if (!candis.length) return ;
12+
console.log(candis, resArr, targ);
13+
const ele = candis[0];
14+
15+
if (targ === ele) {
16+
res.push([...resArr, ele]);
17+
} else {
18+
calCombs(candis.slice(1), [...resArr], targ);
19+
}
20+
21+
if (targ - ele > 0) {
22+
calCombs(candis, [...resArr, ele], targ-ele);
23+
}
24+
}
25+
26+
calCombs(candidates, [], target);
27+
28+
console.log(res);
29+
return res;
30+
};
31+
32+
combinationSum([2,3,6,7],
33+
7);

src/letter-combinations-of-a-phone-number/res.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,32 @@ let letterCombinations = function(digits) {
4747
}
4848

4949
return res;
50-
};
50+
};
51+
52+
/**
53+
* @param {string} digits
54+
* @return {string[]}
55+
*/
56+
var letterCombinations_2 = function(digits) {
57+
const mapping = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
58+
const res = [];
59+
const len = digits.length;
60+
if (len < 1) return [];
61+
62+
const getCombs = (prefix, start, end) => {
63+
const digitMap = mapping[digits[start]];
64+
// console.log(prefix, start, end, digitMap);
65+
if (start === end) {
66+
return digitMap ? digitMap.split('').map(subfix => {
67+
return prefix + subfix;
68+
}) : [prefix];
69+
} else {
70+
const subCombs = digitMap ? digitMap.split('').map(subfix => getCombs(prefix+subfix, start + 1, end)) : [getCombs(prefix, start + 1, end)];
71+
return subCombs.reduce((acc, cur) => {
72+
return acc.concat(cur);
73+
}, []);
74+
}
75+
}
76+
77+
return res.concat(getCombs('', 0, len-1));
78+
};

src/maximum-subarray/res.js

+11
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@ var maxSubArray = function(nums) {
2020

2121
return ans;
2222
};
23+
24+
25+
/**
26+
* @param {number[]} nums
27+
* @return {number}
28+
*/
29+
var maxSubArray = function(nums) {
30+
if (nums.length < 1) return 0;
31+
32+
return getMaxSum
33+
};

0 commit comments

Comments
 (0)