Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
neetcode-gh merged 20 commits intoneetcode-gh:mainfromloczek:main
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
20 commits
Select commitHold shift + click to select a range
c23d249
add 2-add-two-numbers
loczekJul 6, 2022
1f592b3
add 5-Longest-Palindromic-Substring
loczekJul 6, 2022
a6ffd6f
add 17-Letter-Combinations-of-a-Phone-Number
loczekJul 6, 2022
bde7f31
add 22-Generate-Parentheses
loczekJul 6, 2022
ebc7d7a
add 44-Permutations
loczekJul 6, 2022
91ce4eb
add 62-Unique-Paths
loczekJul 6, 2022
95c87ff
add 78-Subsets
loczekJul 6, 2022
11f7f0d
add 91-Decode-Ways
loczekJul 6, 2022
a0769b0
add 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal
loczekJul 6, 2022
a09546f
add 131-Palindrome-Partitioning
loczekJul 6, 2022
c61789b
add 198-House-Robber
loczekJul 6, 2022
955dc86
add 202-Happy-Number
loczekJul 6, 2022
1b59ccb
add 208-Implement-Trie
loczekJul 6, 2022
8c2b30a
add 211-Design-Add-and-Search-Words-Data-Structure
loczekJul 6, 2022
9b78240
add 230-Kth-Smallest-Element-in-a-BST
loczekJul 6, 2022
61e70a5
add 287-Find-the-Duplicate-Number
loczekJul 6, 2022
d093d65
add 647-Palindromic-Substrings
loczekJul 6, 2022
de3a58e
add 746-Min-Cost-Climbing-Stairs
loczekJul 6, 2022
83b7926
add 853-Car-Fleet
loczekJul 6, 2022
7256e8c
add 1448-Count-Good-Nodes-in-Binary-Tree
loczekJul 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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;
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp