Skip to content

Commit e80c1c3

Browse files
committed
restore javascript files
1 parent a076cfe commit e80c1c3

File tree

156 files changed

+6391
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

156 files changed

+6391
-7
lines changed

.github/workflows/updateCompletionTable.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -508,24 +508,30 @@ const buildTableColumn = (
508508
}
509509

510510
tableMatrix[0].push(language);
511-
for (const [index, [, problemNumber]] of Object.entries(
512-
Object.values(problems)
513-
)) {
514-
tableMatrix[+index + 1].push(checkbox[problemNumber]);
511+
for (const [index, complete] of Object.entries(Object.values(checkbox))) {
512+
tableMatrix[+index + 1].push(complete);
515513
}
516514
};
517515

518516
const makeMarkdown = (table, urls) => {
519517
return [
520518
table[0]
521-
.map((cell) => `<sub>${FOLDER_TO_LANG[cell] || cell}</sub>`)
519+
.map(
520+
(cell) =>
521+
`<small><small><small>${
522+
FOLDER_TO_LANG[cell] || cell
523+
}</small></small></small>`
524+
)
522525
.join(' | '),
523526
table[0].map(() => '----').join(' | '),
524527
...table.slice(1).map((row, rowIndex) =>
525528
row
526529
.map((cell, index) => {
527-
if (index == 0) return `<sub>[${cell}](${urls[rowIndex]})</sub>`;
528-
return `<sub><div align='center'>${cell ? "✔️" : "❌"}</div></sub>`
530+
if (index == 0)
531+
return `<small><small><small>[${cell}](${urls[rowIndex]})</small></small></small>`;
532+
return cell
533+
? "<small><div align='center'>✔️</div></small>"
534+
: "<small><div align='center'>❌</div></small>";
529535
})
530536
.join(' | ')
531537
),

README.md

+155
Large diffs are not rendered by default.

javascript/1-Two-Sum.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function (nums, target) {
7+
let map = {};
8+
for (let i = 0; i < nums.length; i++) {
9+
if (target - nums[i] in map) {
10+
return [map[target - nums[i]], i];
11+
} else {
12+
map[nums[i]] = i;
13+
}
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
var isMatch = function(s, p) {
7+
var lenS = s.length;
8+
var lenP = p.length;
9+
var map = {};
10+
11+
return check(0, 0);
12+
13+
function check(idxS, idxP) {
14+
if (map[idxS + ':' + idxP] !== undefined) {
15+
return map[idxS + ':' + idxP];
16+
}
17+
18+
if (idxS > lenS) {
19+
return false;
20+
}
21+
22+
if (idxS === lenS && idxP === lenP) {
23+
return true;
24+
}
25+
26+
if (p[idxP] === '.' || p[idxP] === s[idxS]) {
27+
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
28+
check(idxS + 1, idxP) || check(idxS, idxP + 2) :
29+
check(idxS + 1, idxP + 1);
30+
} else {
31+
map[idxS + ':' + idxP] = p[idxP + 1] === '*' ?
32+
check(idxS, idxP + 2) : false;
33+
}
34+
35+
return map[idxS + ':' + idxP];
36+
}
37+
};

javascript/100-Same-Tree.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function (p, q) {
15+
if (!p && !q) return true;
16+
if (!p || !q || p.val !== q.val) return false;
17+
return isSameTree(p.right, q.right) && isSameTree(p.left, q.left);
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[][]}
12+
*/
13+
var levelOrder = function (root) {
14+
if (!root) return [];
15+
16+
const result = [];
17+
const queue = [root];
18+
19+
while (queue.length) {
20+
const numNodes = queue.length;
21+
const temp = [];
22+
for (let i = 0; i < numNodes; i++) {
23+
const subtree = queue.shift();
24+
temp.push(subtree.val);
25+
if (subtree.left !== null) queue.push(subtree.left);
26+
if (subtree.right !== null) queue.push(subtree.right);
27+
}
28+
result.push(temp);
29+
}
30+
31+
return result;
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = (root) => {
14+
let maxDepth = 0;
15+
let DFS = (node, depth) => {
16+
if (!node) return maxDepth;
17+
if (depth > maxDepth) maxDepth = depth;
18+
DFS(node.right, depth + 1);
19+
DFS(node.left, depth + 1);
20+
};
21+
DFS(root, 1);
22+
return maxDepth;
23+
};

javascript/1046-Last-Stone-Weight.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class Heap {
2+
constructor(stones) {
3+
this.heap = stones;
4+
this.size = stones.length;
5+
this.heapify(0);
6+
}
7+
right(pos) {
8+
return 2 * pos + 2;
9+
}
10+
left(pos) {
11+
return 2 * pos + 1;
12+
}
13+
isleaf(pos) {
14+
if (2 * pos + 1 >= this.size) return true;
15+
return false;
16+
}
17+
swap(a, b) {
18+
let temp = this.heap[a];
19+
this.heap[a] = this.heap[b];
20+
this.heap[b] = temp;
21+
}
22+
fix(pos) {
23+
if (this.isleaf(pos)) return;
24+
let left = this.left(pos);
25+
let right = this.right(pos);
26+
let bigger = left;
27+
if (right < this.size)
28+
bigger = this.heap[left] > this.heap[right] ? left : right;
29+
if (this.heap[pos] < this.heap[bigger]) {
30+
this.swap(pos, bigger);
31+
this.fix(bigger);
32+
}
33+
}
34+
heapify(pos) {
35+
if (this.isleaf(pos)) return;
36+
this.heapify(this.left(pos));
37+
this.heapify(this.right(pos));
38+
this.fix(pos);
39+
}
40+
delete() {
41+
this.swap(0, --this.size);
42+
this.fix(0);
43+
return this.heap[0];
44+
}
45+
insert(val) {
46+
this.size++;
47+
this.heap[this.size - 1] = val;
48+
this.heapify(0);
49+
}
50+
peek() {
51+
return this.heap[0];
52+
}
53+
}
54+
/**
55+
* @param {number[]} stones
56+
* @return {number}
57+
*/
58+
var lastStoneWeight = function (stones) {
59+
//use a max heap to pop off the top 2 stones at each time
60+
//if result is 0 we can do nothing
61+
//if the result is a new weight we can push it back to the heap
62+
const heap = new Heap(stones);
63+
while (heap.size > 1) {
64+
let x = heap.peek();
65+
heap.delete();
66+
let y = heap.peek();
67+
heap.delete();
68+
const res = x - y;
69+
if (res > 0) heap.insert(res);
70+
}
71+
if (heap.size) return heap.peek();
72+
return 0;
73+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function buildTree(preorder, inorder) {
2+
if (!preorder.length || !inorder.length) return null;
3+
4+
let root = new TreeNode(preorder[0]);
5+
let mid = inorder.indexOf(preorder[0]);
6+
7+
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
8+
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
9+
return root;
10+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/
3+
* Time O(N) | Space(1)
4+
* @param {number[]} height
5+
* @return {number}
6+
*/
7+
var maxArea = function(height) {
8+
let [ left, right, max ] = [ 0, (height.length - 1), 0 ];
9+
10+
while (left < right) {
11+
const [ leftHeight, rightHeight ] = getHeights(height, left, right);
12+
const area = getArea(height, left, right);
13+
14+
max = Math.max(max, area);
15+
16+
const isRightGreater = leftHeight <= rightHeight;
17+
if (isRightGreater) left++;
18+
19+
const isRightLess = rightHeight < leftHeight;
20+
if (isRightLess) right--;
21+
}
22+
23+
return max;
24+
};
25+
26+
const getHeights = (height, left, right) => [ height[left], height[right] ];
27+
28+
const getArea = (height, left, right) => {
29+
const [ leftHeight, rightHeight ] = getHeights(height, left, right);
30+
const _height = Math.min(leftHeight, rightHeight);
31+
const width = right - left;
32+
33+
return _height * width;
34+
};
35+
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {boolean}
12+
*/
13+
var isBalanced = function (root) {
14+
const getHeight = (root) => {
15+
if (!root) return [-1, true];
16+
17+
const [leftHeight, leftBalanced] = getHeight(root.left);
18+
const [rightHeight, rightBalanced] = getHeight(root.right);
19+
20+
const balanced =
21+
leftBalanced &&
22+
rightBalanced &&
23+
Math.abs(leftHeight - rightHeight) < 2;
24+
25+
return [1 + Math.max(leftHeight, rightHeight), balanced];
26+
};
27+
28+
const balanced = getHeight(root)[1];
29+
30+
return balanced;
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var longestCommonSubsequence = function (text1, text2) {
2+
let m = text1.length,
3+
n = text2.length,
4+
DP = new Array(m + 1).fill(0).map((_) => new Array(n + 1).fill(0));
5+
6+
for (let x = m - 1; x >= 0; x--)
7+
for (let y = n - 1; y >= 0; y--) {
8+
if (text1[x] === text2[y]) {
9+
DP[x][y] = 1 + DP[x + 1][y + 1];
10+
} else {
11+
DP[x][y] = Math.max(DP[x + 1][y], DP[x][y + 1]);
12+
}
13+
}
14+
15+
return DP[0][0];
16+
};

0 commit comments

Comments
 (0)