Skip to content

Commit 0b460eb

Browse files
committed
add new scripts
1 parent d49986f commit 0b460eb

16 files changed

+455
-0
lines changed

11-container-with-most-water.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
const maxArea = function(height) {
6+
const arr = [];
7+
const arr_len = height.length;
8+
for (let i = 0, j = arr_len - 1; i < j; ) {
9+
arr.push(Math.abs(j - i) * Math.min(height[i], height[j]));
10+
if (height[i] < height[j]) {
11+
i++;
12+
} else {
13+
j--;
14+
}
15+
}
16+
17+
return Math.max.apply(Math, arr);
18+
};

12-integer-to-roman.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} num
3+
* @return {string}
4+
*/
5+
const map = {
6+
"1000": "M",
7+
"900": "CM",
8+
"500": "D",
9+
"400": "CD",
10+
"100": "C",
11+
"90": "XC",
12+
"50": "L",
13+
"40": "XL",
14+
"10": "X",
15+
"9": "IX",
16+
"5": "V",
17+
"4": "IV",
18+
"1": "I"
19+
};
20+
const intToRoman = function(number) {
21+
const l = fkey(map, number);
22+
if (number == +l) {
23+
return map[number];
24+
}
25+
return map[l] + intToRoman(number - +l);
26+
};
27+
28+
function fkey(m, num) {
29+
const keys = Object.keys(m);
30+
const sArr = keys.filter(el => +el <= num);
31+
return +Math.max.apply(Math, sArr);
32+
}

14-longest-common-prefix.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string}
4+
*/
5+
const longestCommonPrefix = function(strs) {
6+
const A = strs.concat().sort(),
7+
a1 = A[0] || "",
8+
a2 = A[A.length - 1] || "",
9+
L = a1.length,
10+
i = 0;
11+
while (i < L && a1.charAt(i) === a2.charAt(i)) i++;
12+
return a1.substring(0, i);
13+
};

15-3sum.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
const threeSum = function(nums) {
6+
nums = nums.sort((a, b) => a - b);
7+
const res = [];
8+
let lo, hi, sum;
9+
for (let i = 0; i < nums.length - 2; i++) {
10+
if (nums[i] > 0) break;
11+
if (nums[i] === nums[i - 1]) continue;
12+
if (i === 0 || (i > 0 && nums[i] !== nums[i - 1])) {
13+
lo = i + 1;
14+
hi = nums.length - 1;
15+
sum = 0 - nums[i];
16+
while (lo < hi) {
17+
if (nums[lo] + nums[hi] === sum) {
18+
res.push([nums[i], nums[lo], nums[hi]]);
19+
while (lo < hi && nums[lo] === nums[lo + 1]) {
20+
lo += 1;
21+
}
22+
while (lo < hi && nums[hi] === nums[hi - 1]) {
23+
hi -= 1;
24+
}
25+
lo += 1;
26+
hi -= 1;
27+
} else if (nums[lo] + nums[hi] < sum) {
28+
lo++;
29+
} else {
30+
hi--;
31+
}
32+
}
33+
}
34+
}
35+
return res;
36+
};

16-3sum-closest.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
const threeSumClosest = function(nums, target) {
7+
const nums = nums.sort((a, b) => a - b);
8+
let result;
9+
let lo;
10+
let hi;
11+
let sum;
12+
result = nums[0] + nums[1] + nums[nums.length - 1];
13+
for (let i = 0; i < nums.length - 2; i++) {
14+
lo = i + 1;
15+
hi = nums.length - 1;
16+
while (lo < hi) {
17+
sum = nums[i] + nums[lo] + nums[hi];
18+
if (sum < target) {
19+
while (lo < hi && nums[lo] === nums[lo + 1]) {
20+
lo += 1;
21+
}
22+
lo += 1;
23+
} else if (sum > target) {
24+
while (lo < hi && nums[hi] === nums[hi - 1]) {
25+
hi -= 1;
26+
}
27+
hi -= 1;
28+
} else {
29+
return sum;
30+
}
31+
32+
if (Math.abs(target - sum) < Math.abs(target - result)) {
33+
result = sum;
34+
}
35+
}
36+
}
37+
38+
return result;
39+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} digits
3+
* @return {string[]}
4+
*/
5+
const letterCombinations = function(digits) {
6+
if (digits === "") {
7+
return [];
8+
}
9+
const charMap = {
10+
2: ["a", "b", "c"],
11+
3: ["d", "e", "f"],
12+
4: ["g", "h", "i"],
13+
5: ["j", "k", "l"],
14+
6: ["m", "n", "o"],
15+
7: ["p", "q", "r", "s"],
16+
8: ["t", "u", "v"],
17+
9: ["w", "x", "y", "z"]
18+
};
19+
const res = [];
20+
const matrix = [];
21+
for (let i = 0; i < digits.length; i++) {
22+
matrix.push(charMap[digits.charAt(i)]);
23+
}
24+
let tmp = matrix[0];
25+
for (let j = 1; j < matrix.length; j++) {
26+
tmp = helper(matrix, j, tmp);
27+
}
28+
return tmp;
29+
};
30+
function helper(matrix, rowIdx, arr) {
31+
const res = [];
32+
for (let i = 0; i < arr.length; i++) {
33+
const preStr = arr[i];
34+
for (let j = 0; j < matrix[rowIdx].length; j++) {
35+
res.push(`${preStr}${matrix[rowIdx][j]}`);
36+
}
37+
}
38+
return res;
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @param {number} n
11+
* @return {ListNode}
12+
*/
13+
const removeNthFromEnd = (head, n) => {
14+
if (head.next === null) return null;
15+
16+
let ptrBeforeN = head;
17+
let count = 1;
18+
19+
// While there are more elements
20+
let el = head.next;
21+
while (el !== null) {
22+
if (count > n) ptrBeforeN = ptrBeforeN.next;
23+
el = el.next;
24+
count++;
25+
}
26+
27+
if (count === n) return head.next;
28+
29+
ptrBeforeN.next = ptrBeforeN.next.next;
30+
return head;
31+
};

20-valid-parentheses.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
const isValid = function(s) {
6+
const openBrackets = ["(", "{", "["];
7+
const closeBrackets = [")", "}", "]"];
8+
const oArr = [];
9+
let char = "";
10+
let cidx = 0;
11+
let oidx = 0;
12+
for (let i = 0; i < s.length; i++) {
13+
char = s.charAt(i);
14+
if (closeBrackets.indexOf(char) !== -1) {
15+
cidx = closeBrackets.indexOf(char);
16+
lastOpenBracket = oArr[oArr.length - 1];
17+
oidx = openBrackets.indexOf(lastOpenBracket);
18+
if (cidx === oidx) {
19+
oArr.pop();
20+
} else {
21+
return false;
22+
}
23+
} else {
24+
oArr.push(char);
25+
}
26+
}
27+
return oArr.length > 0 ? false : true;
28+
};

21-merge-two-sorted-lists.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
14+
const mergeTwoLists = function(l1, l2) {
15+
if (l1 === null) return l2;
16+
if (l2 === null) return l1;
17+
if (l1.val < l2.val) {
18+
l1.next = mergeTwoLists(l1.next, l2);
19+
return l1;
20+
} else {
21+
l2.next = mergeTwoLists(l1, l2.next);
22+
return l2;
23+
}
24+
};

22-generate-parentheses.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
const generateParenthesis = function(n) {
6+
const res = [];
7+
backtrack(res, "", 0, 0, n);
8+
return res;
9+
};
10+
function backtrack(arr, cur, open, close, max) {
11+
if (cur.length === max * 2) {
12+
arr.push(cur);
13+
return;
14+
}
15+
if (open < max) backtrack(arr, cur + "(", open + 1, close, max);
16+
if (close < open) backtrack(arr, cur + ")", open, close + 1, max);
17+
}

0 commit comments

Comments
 (0)