Skip to content

Commit 11839ef

Browse files
authored
Merge pull request neetcode-gh#363 from loczek/main
Created javascript solutions for 20 problems
2 parents ce69c74 + 7256e8c commit 11839ef

20 files changed

+508
-0
lines changed
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function partition(s) {
2+
let res = [];
3+
let part = [];
4+
5+
function dfs(i) {
6+
if (i >= s.length) {
7+
res.push(part.slice());
8+
return;
9+
}
10+
11+
for (let j = i; j < s.length; j++) {
12+
if (isPali(s, i, j)) {
13+
part.push(s.slice(i, j + 1));
14+
dfs(j + 1);
15+
part.pop();
16+
}
17+
}
18+
}
19+
20+
dfs(0);
21+
return res;
22+
23+
function isPali(s, l, r) {
24+
while (l < r) {
25+
if (s[l] != s[r]) {
26+
return false;
27+
}
28+
l = l + 1;
29+
r = r - 1;
30+
}
31+
return true;
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function goodNodes(root) {
2+
let total = 0;
3+
4+
function traverse(node, prev) {
5+
if (!node) return;
6+
7+
if (node.left || node.right) {
8+
traverse(node.left, Math.max(node.val, prev));
9+
traverse(node.right, Math.max(node.val, prev));
10+
}
11+
12+
if (node.val >= prev) {
13+
total += 1;
14+
}
15+
}
16+
17+
traverse(root, root.val);
18+
19+
return total;
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function letterCombinations(digits) {
2+
let res = [];
3+
4+
const digitToChar = {
5+
2: "abc",
6+
3: "def",
7+
4: "ghi",
8+
5: "jkl",
9+
6: "mno",
10+
7: "qprs",
11+
8: "tuv",
12+
9: "wxyz",
13+
};
14+
15+
function backtrack(i, curStr) {
16+
if (curStr.length === digits.length) {
17+
res.push(curStr);
18+
return;
19+
}
20+
21+
for (const c of digitToChar[digits[i]]) {
22+
backtrack(i + 1, curStr + c);
23+
}
24+
}
25+
26+
if (digits) {
27+
backtrack(0, "");
28+
}
29+
30+
return res;
31+
}

javascript/198-House-Robber.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function rob(nums) {
2+
let rob1 = 0;
3+
let rob2 = 0;
4+
5+
for (const n of nums) {
6+
let temp = Math.max(n + rob1, rob2);
7+
rob1 = rob2;
8+
rob2 = temp;
9+
}
10+
11+
return rob2;
12+
}

javascript/2-Add-Two-Numbers.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function addTwoNumbers(l1, l2) {
2+
let temp1 = l1;
3+
let temp2 = l2;
4+
5+
let dummy = new ListNode(0);
6+
let merged = dummy;
7+
8+
let carry = 0;
9+
10+
while (temp1 || temp2) {
11+
let sum = (temp1?.val || 0) + (temp2?.val || 0) + carry;
12+
carry = Math.floor(sum / 10);
13+
sum = sum % 10;
14+
15+
merged.next = new ListNode(sum);
16+
merged = merged.next;
17+
18+
if (temp1) temp1 = temp1?.next;
19+
if (temp2) temp2 = temp2?.next;
20+
}
21+
22+
if (carry > 0) {
23+
merged.next = new ListNode(carry);
24+
}
25+
26+
return dummy.next;
27+
}

javascript/202-Happy-Number.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function isHappy(n) {
2+
const visit = new Set();
3+
4+
while (!visit.has(n)) {
5+
visit.add(n);
6+
n = sumOfSquares(n);
7+
8+
if (n == 1) return true;
9+
}
10+
11+
return false;
12+
}
13+
14+
function sumOfSquares(n) {
15+
let output = 0;
16+
17+
while (n) {
18+
let digit = n % 10;
19+
digit = digit ** 2;
20+
output += digit;
21+
n = Math.floor(n / 10);
22+
}
23+
24+
return output;
25+
}

javascript/208-Implement-Trie.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class TrieNode {
2+
constructor() {
3+
this.children = {};
4+
this.endOfWord = false;
5+
}
6+
}
7+
8+
class Trie {
9+
constructor() {
10+
this.root = new TrieNode();
11+
}
12+
13+
insert(word) {
14+
let cur = this.root;
15+
16+
for (const c of word) {
17+
if (!(c in cur.children)) {
18+
cur.children[c] = new TrieNode();
19+
}
20+
cur = cur.children[c];
21+
}
22+
cur.endOfWord = true;
23+
}
24+
25+
search(word) {
26+
let cur = this.root;
27+
28+
for (const c of word) {
29+
if (!(c in cur.children)) {
30+
return false;
31+
}
32+
cur = cur.children[c];
33+
}
34+
35+
return cur.endOfWord;
36+
}
37+
38+
startsWith(prefix) {
39+
let cur = this.root;
40+
41+
for (const c of prefix) {
42+
if (!(c in cur.children)) {
43+
return false;
44+
}
45+
cur = cur.children[c];
46+
}
47+
48+
return true;
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class TrieNode {
2+
constructor() {
3+
this.children = {};
4+
this.endOfWord = false;
5+
}
6+
}
7+
8+
class WordDictionary {
9+
constructor() {
10+
this.root = new TrieNode();
11+
}
12+
13+
addWord(word) {
14+
let cur = this.root;
15+
16+
for (const c of word) {
17+
if (!(c in cur.children)) {
18+
cur.children[c] = new TrieNode();
19+
}
20+
cur = cur.children[c];
21+
}
22+
cur.endOfWord = true;
23+
}
24+
25+
search(word) {
26+
function dfs(j, root) {
27+
let cur = root;
28+
29+
for (let i = j; i < word.length; i++) {
30+
const c = word[i];
31+
32+
if (c === ".") {
33+
for (const key in cur.children) {
34+
if (Object.prototype.hasOwnProperty.call(cur.children, key)) {
35+
const child = cur.children[key];
36+
37+
if (dfs(i + 1, child)) {
38+
return true;
39+
}
40+
}
41+
}
42+
return false;
43+
} else {
44+
if (!(c in cur.children)) {
45+
return false;
46+
}
47+
cur = cur.children[c];
48+
}
49+
}
50+
51+
return cur.endOfWord;
52+
}
53+
return dfs(0, this.root);
54+
}
55+
}

javascript/22-Generate-Parentheses.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function generateParenthesis(n) {
2+
const stack = [];
3+
const res = [];
4+
5+
function backtrack(openN, closedN) {
6+
if (openN === n && closedN === n) {
7+
res.push(stack.join(""));
8+
return;
9+
}
10+
11+
if (openN < n) {
12+
backtrack(openN + 1, closedN);
13+
stack.pop();
14+
stack.push("(");
15+
}
16+
17+
if (closedN < openN) {
18+
backtrack(openN, closedN + 1);
19+
stack.push(")");
20+
stack.pop();
21+
}
22+
}
23+
24+
backtrack(0, 0);
25+
26+
return res;
27+
}

0 commit comments

Comments
 (0)