Skip to content

Commit d49986f

Browse files
committed
add scripts.
1 parent 345d5cc commit d49986f

22 files changed

+432
-189
lines changed

10-regular-expression-matching.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
const isMatch = function(s, p) {
7+
let memory = new Array(s.length + 1)
8+
.fill(0)
9+
.map(e => new Array(p.length + 1).fill(-1));
10+
return memorySearch(s, 0, p, 0, memory);
11+
};
12+
13+
const memorySearch = (s, i, p, k, memory) => {
14+
if (memory[i][k] != -1) return memory[i][k];
15+
if (k == p.length) return i == s.length;
16+
17+
let firstMatch = i < s.length && (s[i] == p[k] || p[k] == ".");
18+
if (k + 1 < p.length && p[k + 1] == "*") {
19+
memory[i][k] =
20+
(firstMatch && memorySearch(s, i + 1, p, k, memory)) ||
21+
memorySearch(s, i, p, k + 2, memory);
22+
} else {
23+
memory[i][k] = firstMatch && memorySearch(s, i + 1, p, k + 1, memory);
24+
}
25+
26+
return memory[i][k];
27+
};

260-single-number-III.js

100644100755
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* @return {number[]}
44
*/
55
const singleNumber = function(nums) {
6-
const hash = {}
7-
nums.forEach((el, idx) => {
8-
if(hash.hasOwnProperty(el)) {
9-
hash[el] += 1
10-
delete hash[el]
11-
} else {
12-
hash[el] = 1
13-
}
14-
})
15-
return Object.keys(hash).map(el => +el)
16-
};
6+
const hash = {};
7+
nums.forEach((el, idx) => {
8+
if (hash.hasOwnProperty(el)) {
9+
hash[el] += 1;
10+
delete hash[el];
11+
} else {
12+
hash[el] = 1;
13+
}
14+
});
15+
return Object.keys(hash).map(el => +el);
16+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const lengthOfLongestSubstring = function(s) {
6+
// var p=0, q=0; //p: start of the sub, q: end of the queue
7+
8+
//hashmap in js????? Array.indexOf
9+
const sub = [];
10+
let max = 0;
11+
12+
for (let i = 0; i < s.length; i++) {
13+
let index = sub.indexOf(s.charAt(i));
14+
if (index == -1) {
15+
sub.push(s.charAt(i));
16+
// q++;
17+
} else {
18+
//find repeat, get index of repeat el, remve all el before that index
19+
sub = sub.slice(index + 1, sub.length);
20+
sub.push(s.charAt(i));
21+
}
22+
max = Math.max(max, sub.length);
23+
}
24+
return max;
25+
};

4-median-of-two-sorted-arrays.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {number[]} A
3+
* @param {number[]} B
4+
* @return {number}
5+
*/
6+
7+
const findMedianSortedArrays = function(A, B) {
8+
let m = A.length,
9+
n = B.length;
10+
11+
if (m > n) {
12+
return findMedianSortedArrays(B, A);
13+
}
14+
15+
let imin = 0,
16+
imax = m,
17+
i,
18+
j;
19+
while (imin <= imax) {
20+
i = (imin + imax) >> 1;
21+
j = ((m + n + 1) >> 1) - i;
22+
if (j > 0 && i < m && B[j - 1] > A[i]) {
23+
imin = i + 1;
24+
} else if (i > 0 && j < n && A[i - 1] > B[j]) {
25+
imax = i - 1;
26+
} else {
27+
if (i === 0) {
28+
num1 = B[j - 1];
29+
} else if (j === 0) {
30+
num1 = A[i - 1];
31+
} else {
32+
num1 = Math.max(A[i - 1], B[j - 1]);
33+
}
34+
35+
if ((m + n) & 1) {
36+
return num1;
37+
}
38+
39+
if (i === m) {
40+
num2 = B[j];
41+
} else if (j === n) {
42+
num2 = A[i];
43+
} else {
44+
num2 = Math.min(A[i], B[j]);
45+
}
46+
return (num1 + num2) / 2.0;
47+
}
48+
}
49+
};

406-queue-reconstruction-by-height.js

100644100755
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
* @return {number[][]}
44
*/
55
const reconstructQueue = function(people) {
6-
if (!people) return []
7-
const peopledct = {}
8-
let height = []
9-
const res = []
10-
people.forEach((el, idx) => {
11-
if (peopledct.hasOwnProperty(el[0])) {
12-
peopledct[el[0]].push([el[1], idx])
13-
} else {
14-
peopledct[el[0]] = [[el[1], idx]]
15-
height.push(el[0])
16-
}
17-
})
18-
height = height.sort((a, b) => b - a)
6+
if (!people) return [];
7+
const peopledct = {};
8+
let height = [];
9+
const res = [];
10+
people.forEach((el, idx) => {
11+
if (peopledct.hasOwnProperty(el[0])) {
12+
peopledct[el[0]].push([el[1], idx]);
13+
} else {
14+
peopledct[el[0]] = [[el[1], idx]];
15+
height.push(el[0]);
16+
}
17+
});
18+
height = height.sort((a, b) => b - a);
1919

20-
for (let i = 0; i < height.length; i++) {
21-
peopledct[height[i]] = peopledct[height[i]].sort((a, b) => a[0] - b[0])
22-
for (el of peopledct[height[i]]) {
23-
res.splice(el[0],0, people[el[1]])
24-
}
20+
for (let i = 0; i < height.length; i++) {
21+
peopledct[height[i]] = peopledct[height[i]].sort((a, b) => a[0] - b[0]);
22+
for (el of peopledct[height[i]]) {
23+
res.splice(el[0], 0, people[el[1]]);
2524
}
26-
return res
25+
}
26+
return res;
2727
};
2828

29-
console.log(reconstructQueue([[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]))
29+
console.log(reconstructQueue([[7, 0], [4, 4], [7, 1], [5, 0], [6, 1], [5, 2]]));

496-next-greater-element-I.js

100644100755
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
* @return {number[]}
55
*/
66
const nextGreaterElement = function(findNums, nums) {
7-
const map = {}
8-
const stack = []
9-
for(let num of nums) {
10-
while(stack.length && stack[stack.length - 1] < num) {
11-
let tmp = stack.pop()
12-
map[tmp] = num
13-
}
14-
stack.push(num)
15-
}
16-
for (let i = 0; i < findNums.length; i++) {
17-
findNums[i] = map[findNums[i]] == null ? -1 : map[findNums[i]]
7+
const map = {};
8+
const stack = [];
9+
for (let num of nums) {
10+
while (stack.length && stack[stack.length - 1] < num) {
11+
let tmp = stack.pop();
12+
map[tmp] = num;
1813
}
14+
stack.push(num);
15+
}
16+
for (let i = 0; i < findNums.length; i++) {
17+
findNums[i] = map[findNums[i]] == null ? -1 : map[findNums[i]];
18+
}
1919

20-
return findNums
20+
return findNums;
2121
};
2222

23-
console.log(nextGreaterElement([4,1,2], [1,3,4,2]))
24-
console.log(nextGreaterElement([2,4], [1,2,3,4]))
25-
console.log(nextGreaterElement([1,2,3], [9, 8, 7, 3, 2, 1, 6]))
23+
console.log(nextGreaterElement([4, 1, 2], [1, 3, 4, 2]));
24+
console.log(nextGreaterElement([2, 4], [1, 2, 3, 4]));
25+
console.log(nextGreaterElement([1, 2, 3], [9, 8, 7, 3, 2, 1, 6]));

5-longest-palindromic-substring.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
const longestPalindrome = function(s) {
6+
let T = preProcess(s);
7+
let n = T.length;
8+
let P = [];
9+
let C = 0,
10+
R = 0;
11+
let i_mirror;
12+
for (let i = 1; i < n - 1; i++) {
13+
i_mirror = 2 * C - i; // equals to i' = C - (i-C)
14+
15+
P[i] = R > i ? Math.min(R - i, P[i_mirror]) : 0;
16+
17+
// Attempt to expand palindrome centered at i
18+
while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) P[i]++;
19+
20+
// If palindrome centered at i expand past R,
21+
// adjust center based on expanded palindrome.
22+
if (i + P[i] > R) {
23+
C = i;
24+
R = i + P[i];
25+
}
26+
}
27+
28+
// Find the maximum element in P.
29+
let maxLen = 0;
30+
let centerIndex = 0;
31+
for (let j = 1; j < n - 1; j++) {
32+
if (P[j] > maxLen) {
33+
maxLen = P[j];
34+
centerIndex = j;
35+
}
36+
}
37+
38+
return s.substr((centerIndex - 1 - maxLen) / 2, maxLen);
39+
};
40+
41+
function preProcess(s) {
42+
let n = s.length;
43+
if (n === 0) return "^$";
44+
let ret = "^";
45+
for (let i = 0; i < n; i++) ret += "#" + s.substr(i, 1);
46+
47+
ret += "#$";
48+
return ret;
49+
}

513-find-bottom-left-tree-value.js

100644100755
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
* @return {number}
1111
*/
1212
const findBottomLeftValue = function(root) {
13-
const res = []
14-
single(root, 0, res)
15-
return res[res.length - 1][0].val
13+
const res = [];
14+
single(root, 0, res);
15+
return res[res.length - 1][0].val;
1616
};
1717

1818
function single(node, row, arr) {
19-
if (node == null) {
20-
return null
21-
}
22-
if (row < arr.length) {
23-
arr[row].push(node)
24-
} else {
25-
arr[row] = [node]
26-
}
27-
single(node.left, row + 1, arr)
28-
single(node.right, row + 1, arr)
29-
}
19+
if (node == null) {
20+
return null;
21+
}
22+
if (row < arr.length) {
23+
arr[row].push(node);
24+
} else {
25+
arr[row] = [node];
26+
}
27+
single(node.left, row + 1, arr);
28+
single(node.right, row + 1, arr);
29+
}

515-find-largest-value-in-each-tree-row.js

100644100755
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
* @return {number[]}
1111
*/
1212
const largestValues = function(root) {
13-
const res = []
14-
single(root, 0, res)
15-
return res
13+
const res = [];
14+
single(root, 0, res);
15+
return res;
1616
};
1717

1818
function single(node, row, arr) {
19-
if (node == null) {
20-
return null
19+
if (node == null) {
20+
return null;
21+
}
22+
if (row < arr.length) {
23+
if (node.val > arr[row]) {
24+
arr[row] = node.val;
2125
}
22-
if (row < arr.length) {
23-
if (node.val > arr[row]) {
24-
arr[row] = node.val
25-
}
26-
} else {
27-
arr[row] = node.val
28-
}
29-
single(node.left, row + 1, arr)
30-
single(node.right, row + 1, arr)
31-
}
26+
} else {
27+
arr[row] = node.val;
28+
}
29+
single(node.left, row + 1, arr);
30+
single(node.right, row + 1, arr);
31+
}

540-single-element-in-a-sorted-array.js

100644100755
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* @return {number}
44
*/
55
const singleNonDuplicate = function(nums) {
6-
let i=0;
7-
while(true){
8-
if(nums[i]==nums[i+1]) {
9-
i+=2;
10-
} else {
11-
return nums[i];
12-
}
6+
let i = 0;
7+
while (true) {
8+
if (nums[i] == nums[i + 1]) {
9+
i += 2;
10+
} else {
11+
return nums[i];
1312
}
14-
};
13+
}
14+
};

0 commit comments

Comments
 (0)