Skip to content

Commit 9a6a3de

Browse files
committed
add new scripts.
1 parent 9983072 commit 9a6a3de

9 files changed

+290
-0
lines changed

100-same-tree.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} p
10+
* @param {TreeNode} q
11+
* @return {boolean}
12+
*/
13+
const isSameTree = function(p, q) {
14+
return isSame(p, q);
15+
};
16+
17+
const isSame = (p, q) => {
18+
if (p === null && q === null) return true;
19+
20+
if ((p !== null && q === null) || (p === null && q !== null)) return false;
21+
22+
if (p.val !== q.val) return false;
23+
24+
return isSame(p.left, q.left) && isSame(p.right, q.right);
25+
};

104-maximum-depth-of-binary-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) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
const maxDepth = function(root) {
13+
if (!root) return 0;
14+
const left = maxDepth(root.left);
15+
const right = maxDepth(root.right);
16+
let depth = left > right ? left : right;
17+
return (depth += 1);
18+
};

118-pascal's-triangle.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number} numRows
3+
* @return {number[][]}
4+
*/
5+
const generate = function(numRows) {
6+
// row 0 => [1] length 0
7+
// row 1 => [1, 1] length 1
8+
// row 2 => [1, 2, 1] length 2
9+
// row 3 => [1, 3, 3, 1] length 3
10+
11+
// current[i] = prev[i - 1] + prev[i]
12+
13+
const res = [];
14+
for (let row = 0; row < numRows; row += 1) {
15+
if (row === 0) {
16+
res.push([1]);
17+
continue;
18+
}
19+
20+
if (row === 1) {
21+
res.push([1, 1]);
22+
continue;
23+
}
24+
25+
const newRow = [];
26+
const maxIdx = row;
27+
for (let i = 0; i <= maxIdx; i += 1) {
28+
if (i === 0 || i === maxIdx) {
29+
newRow.push(1);
30+
} else {
31+
newRow.push(res[row - 1][i - 1] + res[row - 1][i]);
32+
}
33+
}
34+
res.push(newRow);
35+
}
36+
37+
return res;
38+
};

119-pascal's-triangle-II.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} rowIndex
3+
* @return {number[]}
4+
*/
5+
const getRow = function(rowIndex) {
6+
if (!rowIndex) return [1];
7+
if (rowIndex === 1) return [1, 1];
8+
const res = [1, 1];
9+
for (let i = 2; i <= rowIndex; i++) {
10+
res[i] = 1;
11+
for (let j = i - 1; j >= 1; j--) {
12+
res[j] = res[j] + res[j - 1];
13+
}
14+
}
15+
return res;
16+
};

146-lru-cache.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class Node {
2+
constructor(key, val) {
3+
this.val = val;
4+
this.key = key;
5+
this.next = this.pre = null;
6+
}
7+
}
8+
9+
const LRUCache = function(capacity) {
10+
this.capacity = capacity;
11+
this.count = 0;
12+
this.start = new Node(-1, -1);
13+
this.end = new Node(-1, -1);
14+
this.start.next = this.end;
15+
this.end.pre = this.start;
16+
this.map = {};
17+
};
18+
19+
// insert node into the next of the start
20+
const insertAfter = function(start, node) {
21+
let next = start.next;
22+
start.next = node;
23+
node.pre = start;
24+
node.next = next;
25+
next.pre = node;
26+
};
27+
28+
const detach = function(node) {
29+
let pre = node.pre,
30+
next = node.next;
31+
pre.next = next;
32+
next.pre = pre;
33+
node.next = node.pre = null;
34+
};
35+
36+
/**
37+
* @param {number} key
38+
* @return {number}
39+
*/
40+
LRUCache.prototype.get = function(key) {
41+
let node = this.map[key];
42+
if (node != undefined) {
43+
detach(node);
44+
insertAfter(this.start, node);
45+
return node.val;
46+
} else {
47+
return -1;
48+
}
49+
};
50+
51+
/**
52+
* @param {number} key
53+
* @param {number} value
54+
* @return {void}
55+
*/
56+
LRUCache.prototype.put = function(key, value) {
57+
let node = this.map[key];
58+
if (!node) {
59+
if (this.count == this.capacity) {
60+
// deleting last nodes
61+
let t = this.end.pre;
62+
detach(t);
63+
delete this.map[t.key];
64+
} else {
65+
this.count++;
66+
}
67+
node = new Node(key, value);
68+
this.map[key] = node;
69+
insertAfter(this.start, node);
70+
} else {
71+
node.val = value;
72+
detach(node);
73+
insertAfter(this.start, node);
74+
}
75+
};
76+
77+
/**
78+
* Your LRUCache object will be instantiated and called as such:
79+
* var obj = Object.create(LRUCache).createNew(capacity)
80+
* var param_1 = obj.get(key)
81+
* obj.put(key,value)
82+
*/

151-reverse-words-in-a-string.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {string} str
3+
* @returns {string}
4+
*/
5+
const reverseWords = function(str) {
6+
return str
7+
.trim()
8+
.split(/\s+/)
9+
.reverse()
10+
.join(" ");
11+
};
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} input
3+
* @return {number[]}
4+
*/
5+
const diffWaysToCompute = function(input) {
6+
const res = [];
7+
let left;
8+
let right;
9+
for (let i = 0; i < input.length; i++) {
10+
if (input[i] < "0") {
11+
left = diffWaysToCompute(input.slice(0, i));
12+
right = diffWaysToCompute(input.slice(i + 1));
13+
for (let rl of left) {
14+
for (let rr of right) {
15+
switch (input[i]) {
16+
case "+":
17+
res.push(rl + rr);
18+
break;
19+
case "-":
20+
res.push(rl - rr);
21+
break;
22+
case "*":
23+
res.push(rl * rr);
24+
break;
25+
default:
26+
break;
27+
}
28+
}
29+
}
30+
}
31+
}
32+
if (res.length === 0) {
33+
res.push(+input);
34+
}
35+
return res;
36+
};

445-add-two-numbers-II.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
const addTwoNumbers = function(l1, l2) {
14+
const s1 = [];
15+
const s2 = [];
16+
while (l1 !== null) {
17+
s1.push(l1.val);
18+
l1 = l1.next;
19+
}
20+
while (l2 !== null) {
21+
s2.push(l2.val);
22+
l2 = l2.next;
23+
}
24+
25+
let list = new ListNode(0);
26+
let sum = 0;
27+
while (s1.length > 0 || s2.length > 0) {
28+
if (s1.length > 0) {
29+
sum += s1.pop();
30+
}
31+
if (s2.length > 0) {
32+
sum += s2.pop();
33+
}
34+
list.val = sum % 10;
35+
const head = new ListNode(Math.floor(sum / 10));
36+
head.next = list;
37+
list = head;
38+
sum = Math.floor(sum / 10);
39+
}
40+
41+
return list.val === 0 ? list.next : list;
42+
};
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @return {ListNode}
11+
*/
12+
const deleteDuplicates = function(head) {
13+
let current = head;
14+
while (current !== null && current.next !== null) {
15+
if (current.val === current.next.val) {
16+
current.next = current.next.next;
17+
} else {
18+
current = current.next;
19+
}
20+
}
21+
return head;
22+
};

0 commit comments

Comments
 (0)