- Notifications
You must be signed in to change notification settings - Fork2.4k
Created javascript solutions for 20 problems#363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
20 commits Select commitHold shift + click to select a range
c23d249
add 2-add-two-numbers
loczek1f592b3
add 5-Longest-Palindromic-Substring
loczeka6ffd6f
add 17-Letter-Combinations-of-a-Phone-Number
loczekbde7f31
add 22-Generate-Parentheses
loczekebc7d7a
add 44-Permutations
loczek91ce4eb
add 62-Unique-Paths
loczek95c87ff
add 78-Subsets
loczek11f7f0d
add 91-Decode-Ways
loczeka0769b0
add 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
loczeka09546f
add 131-Palindrome-Partitioning
loczekc61789b
add 198-House-Robber
loczek955dc86
add 202-Happy-Number
loczek1b59ccb
add 208-Implement-Trie
loczek8c2b30a
add 211-Design-Add-and-Search-Words-Data-Structure
loczek9b78240
add 230-Kth-Smallest-Element-in-a-BST
loczek61e70a5
add 287-Find-the-Duplicate-Number
loczekd093d65
add 647-Palindromic-Substrings
loczekde3a58e
add 746-Min-Cost-Climbing-Stairs
loczek83b7926
add 853-Car-Fleet
loczek7256e8c
add 1448-Count-Good-Nodes-in-Binary-Tree
loczekFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletionsjavascript/105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function buildTree(preorder, inorder) { | ||
if (!preorder.length || !inorder.length) return null; | ||
let root = new TreeNode(preorder[0]); | ||
let mid = inorder.indexOf(preorder[0]); | ||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
return root; | ||
} |
33 changes: 33 additions & 0 deletionsjavascript/131-Palindrome-Partitioning.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function partition(s) { | ||
let res = []; | ||
let part = []; | ||
function dfs(i) { | ||
if (i >= s.length) { | ||
res.push(part.slice()); | ||
return; | ||
} | ||
for (let j = i; j < s.length; j++) { | ||
if (isPali(s, i, j)) { | ||
part.push(s.slice(i, j + 1)); | ||
dfs(j + 1); | ||
part.pop(); | ||
} | ||
} | ||
} | ||
dfs(0); | ||
return res; | ||
function isPali(s, l, r) { | ||
while (l < r) { | ||
if (s[l] != s[r]) { | ||
return false; | ||
} | ||
l = l + 1; | ||
r = r - 1; | ||
} | ||
return true; | ||
} | ||
} |
20 changes: 20 additions & 0 deletionsjavascript/1448-Count-Good-Nodes-in-Binary-Tree.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function goodNodes(root) { | ||
let total = 0; | ||
function traverse(node, prev) { | ||
if (!node) return; | ||
if (node.left || node.right) { | ||
traverse(node.left, Math.max(node.val, prev)); | ||
traverse(node.right, Math.max(node.val, prev)); | ||
} | ||
if (node.val >= prev) { | ||
total += 1; | ||
} | ||
} | ||
traverse(root, root.val); | ||
return total; | ||
} |
31 changes: 31 additions & 0 deletionsjavascript/17-Letter-Combinations-of-a-Phone-Number.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function letterCombinations(digits) { | ||
let res = []; | ||
const digitToChar = { | ||
2: "abc", | ||
3: "def", | ||
4: "ghi", | ||
5: "jkl", | ||
6: "mno", | ||
7: "qprs", | ||
8: "tuv", | ||
9: "wxyz", | ||
}; | ||
function backtrack(i, curStr) { | ||
if (curStr.length === digits.length) { | ||
res.push(curStr); | ||
return; | ||
} | ||
for (const c of digitToChar[digits[i]]) { | ||
backtrack(i + 1, curStr + c); | ||
} | ||
} | ||
if (digits) { | ||
backtrack(0, ""); | ||
} | ||
return res; | ||
} |
12 changes: 12 additions & 0 deletionsjavascript/198-House-Robber.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function rob(nums) { | ||
let rob1 = 0; | ||
let rob2 = 0; | ||
for (const n of nums) { | ||
let temp = Math.max(n + rob1, rob2); | ||
rob1 = rob2; | ||
rob2 = temp; | ||
} | ||
return rob2; | ||
} |
27 changes: 27 additions & 0 deletionsjavascript/2-Add-Two-Numbers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function addTwoNumbers(l1, l2) { | ||
let temp1 = l1; | ||
let temp2 = l2; | ||
let dummy = new ListNode(0); | ||
let merged = dummy; | ||
let carry = 0; | ||
while (temp1 || temp2) { | ||
let sum = (temp1?.val || 0) + (temp2?.val || 0) + carry; | ||
carry = Math.floor(sum / 10); | ||
sum = sum % 10; | ||
merged.next = new ListNode(sum); | ||
merged = merged.next; | ||
if (temp1) temp1 = temp1?.next; | ||
if (temp2) temp2 = temp2?.next; | ||
} | ||
if (carry > 0) { | ||
merged.next = new ListNode(carry); | ||
} | ||
return dummy.next; | ||
} |
25 changes: 25 additions & 0 deletionsjavascript/202-Happy-Number.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function isHappy(n) { | ||
const visit = new Set(); | ||
while (!visit.has(n)) { | ||
visit.add(n); | ||
n = sumOfSquares(n); | ||
if (n == 1) return true; | ||
} | ||
return false; | ||
} | ||
function sumOfSquares(n) { | ||
let output = 0; | ||
while (n) { | ||
let digit = n % 10; | ||
digit = digit ** 2; | ||
output += digit; | ||
n = Math.floor(n / 10); | ||
} | ||
return output; | ||
} |
50 changes: 50 additions & 0 deletionsjavascript/208-Implement-Trie.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class TrieNode { | ||
constructor() { | ||
this.children = {}; | ||
this.endOfWord = false; | ||
} | ||
} | ||
class Trie { | ||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
insert(word) { | ||
let cur = this.root; | ||
for (const c of word) { | ||
if (!(c in cur.children)) { | ||
cur.children[c] = new TrieNode(); | ||
} | ||
cur = cur.children[c]; | ||
} | ||
cur.endOfWord = true; | ||
} | ||
search(word) { | ||
let cur = this.root; | ||
for (const c of word) { | ||
if (!(c in cur.children)) { | ||
return false; | ||
} | ||
cur = cur.children[c]; | ||
} | ||
return cur.endOfWord; | ||
} | ||
startsWith(prefix) { | ||
let cur = this.root; | ||
for (const c of prefix) { | ||
if (!(c in cur.children)) { | ||
return false; | ||
} | ||
cur = cur.children[c]; | ||
} | ||
return true; | ||
} | ||
} |
55 changes: 55 additions & 0 deletionsjavascript/211-Design-Add-and-Search-Words-Data-Structure.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class TrieNode { | ||
constructor() { | ||
this.children = {}; | ||
this.endOfWord = false; | ||
} | ||
} | ||
class WordDictionary { | ||
constructor() { | ||
this.root = new TrieNode(); | ||
} | ||
addWord(word) { | ||
let cur = this.root; | ||
for (const c of word) { | ||
if (!(c in cur.children)) { | ||
cur.children[c] = new TrieNode(); | ||
} | ||
cur = cur.children[c]; | ||
} | ||
cur.endOfWord = true; | ||
} | ||
search(word) { | ||
function dfs(j, root) { | ||
let cur = root; | ||
for (let i = j; i < word.length; i++) { | ||
const c = word[i]; | ||
if (c === ".") { | ||
for (const key in cur.children) { | ||
if (Object.prototype.hasOwnProperty.call(cur.children, key)) { | ||
const child = cur.children[key]; | ||
if (dfs(i + 1, child)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} else { | ||
if (!(c in cur.children)) { | ||
return false; | ||
} | ||
cur = cur.children[c]; | ||
} | ||
} | ||
return cur.endOfWord; | ||
} | ||
return dfs(0, this.root); | ||
} | ||
} |
27 changes: 27 additions & 0 deletionsjavascript/22-Generate-Parentheses.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function generateParenthesis(n) { | ||
const stack = []; | ||
const res = []; | ||
function backtrack(openN, closedN) { | ||
if (openN === n && closedN === n) { | ||
res.push(stack.join("")); | ||
return; | ||
} | ||
if (openN < n) { | ||
backtrack(openN + 1, closedN); | ||
stack.pop(); | ||
stack.push("("); | ||
} | ||
if (closedN < openN) { | ||
backtrack(openN, closedN + 1); | ||
stack.push(")"); | ||
stack.pop(); | ||
} | ||
} | ||
backtrack(0, 0); | ||
return res; | ||
} |
21 changes: 21 additions & 0 deletionsjavascript/230-Kth-Smallest-Element-in-a-BST.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function kthSmallest(root, k) { | ||
let n = 0; | ||
const stack = []; | ||
let cur = root; | ||
while (cur || stack) { | ||
while (cur) { | ||
stack.push(cur); | ||
cur = cur.left; | ||
} | ||
cur = stack.pop(); | ||
n += 1; | ||
if (n == k) { | ||
return cur.val; | ||
} | ||
cur = cur?.right; | ||
} | ||
} |
24 changes: 24 additions & 0 deletionsjavascript/287-Find-the-Duplicate-Number.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function findDuplicate(nums) { | ||
let slow = 0; | ||
let fast = 0; | ||
while (true) { | ||
slow = nums[slow]; | ||
fast = nums[nums[fast]]; | ||
if (slow == fast) { | ||
break; | ||
} | ||
} | ||
let slow2 = 0; | ||
while (true) { | ||
slow = nums[slow]; | ||
slow2 = nums[slow2]; | ||
if (slow == slow2) { | ||
break; | ||
} | ||
} | ||
return slow; | ||
} |
25 changes: 25 additions & 0 deletionsjavascript/46-Permutations.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function permute(nums) { | ||
const res = []; | ||
if (nums.length === 1) { | ||
return [nums.slice()]; | ||
} | ||
for (const i of nums) { | ||
let n = nums.shift(); | ||
let perms = permute(nums); | ||
for (const perm of perms) { | ||
perm.push(n); | ||
} | ||
perms.forEach((perm) => { | ||
res.push(perm); | ||
}); | ||
nums.push(n); | ||
} | ||
return res; | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.