Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
1b10a88
Created 3-Longest-Substring-Without-Repeating-Characters
loczek Jul 8, 2022
7cab86c
Created 5-Longest-Palindromic-Substring
loczek Jul 8, 2022
3b541cb
Created 11-Container-With-Most-Water
loczek Jul 8, 2022
ea9e89e
Created 15-3Sum
loczek Jul 8, 2022
3999ae7
Created 17-Letter-Combinations-of-a-Phone-Number
loczek Jul 8, 2022
04b7acc
Created 20-Valid-Parentheses
loczek Jul 8, 2022
134207b
Created 22-Generate-Parentheses
loczek Jul 8, 2022
7b02ff7
Created 36-Valid-Sudoku
loczek Jul 8, 2022
a4a16b9
Created 39-Combination-Sum
loczek Jul 8, 2022
38ce46f
Created 40-Combination-Sum-II
loczek Jul 8, 2022
c6e87c2
Created 42-Trapping-Rain-Water
loczek Jul 8, 2022
2a293a9
Created 46-Permutations
loczek Jul 8, 2022
5a520d3
Created 48-Rotate-Image
loczek Jul 8, 2022
1e4d556
Created 49-Group-Anagrams
loczek Jul 8, 2022
91a08b8
Created 50-Pow
loczek Jul 8, 2022
47c1ba7
Created 53-Maximum-Subarray
loczek Jul 8, 2022
b615b91
Created 54-Spiral-Matrix
loczek Jul 8, 2022
9b2e781
Created 62-Unique-Paths
loczek Jul 8, 2022
975fbe1
Created 66-Plus-One
loczek Jul 8, 2022
c02a71b
Created 70-Climbing-Stairs
loczek Jul 8, 2022
c5ca6f4
Created 74-Search-a-2D-Matrix
loczek Jul 8, 2022
c2141cc
Created 78-Subsets
loczek Jul 8, 2022
1d29bbf
Created 90-Subsets-II
loczek Jul 8, 2022
af9aa75
Created 91-Decode-Ways
loczek Jul 8, 2022
8360454
Created 100-Same-Tree
loczek Jul 8, 2022
c6f7386
Created 102-Binary-Tree-Level-Order-Traversal
loczek Jul 8, 2022
ca7106e
Created 104-Maximum-Depth-of-Binary-Tree
loczek Jul 8, 2022
7b154fe
Created 110-Balanced-Binary-Tree
loczek Jul 8, 2022
eca3a7e
Created 121-Best-Time-To-Buy-and-Sell-Stock
loczek Jul 8, 2022
a223232
Created 125-Valid-Palindrome
loczek Jul 8, 2022
55029d3
Created 128-Longest-Consecutive-Sequence
loczek Jul 8, 2022
583f850
Created 131-Palindrome-Partitioning
loczek Jul 8, 2022
36e6666
Created 136-Single-Number
loczek Jul 8, 2022
50e088c
Created 139-Word-Break
loczek Jul 8, 2022
aa491af
Created 141-Linked-List-Cycle
loczek Jul 8, 2022
9505d40
Created 143-Reorder-List
loczek Jul 8, 2022
d9843b8
Created 150-Evaluate-Reverse-Polish-Notation
loczek Jul 8, 2022
58866b3
Created 152-Maximum-Product-Subarray
loczek Jul 8, 2022
c259b59
Created 155-Min-Stack
loczek Jul 8, 2022
4416651
Created 167-Two-Sum-II
loczek Jul 8, 2022
a67464f
Created 191-Number-of-1-Bits
loczek Jul 8, 2022
6aaade3
Created 198-House-Robber
loczek Jul 8, 2022
bc700fc
Created 202-Happy-Number
loczek Jul 8, 2022
5d178e5
Created 208-Implement-Trie
loczek Jul 8, 2022
8e93755
Created 211-Design-Add-and-Search-Words-Data-Structure
loczek Jul 8, 2022
28d3893
Created 213-House-Robber-II
loczek Jul 8, 2022
5e00823
Created 226-Invert-Binary-Tree
loczek Jul 8, 2022
752ba80
Created 235-Lowest-Common-Ancestor-of-a-Binary Search-Tree
loczek Jul 8, 2022
11aa46a
Created 242-Valid-Anagram
loczek Jul 8, 2022
a81e4e9
Created 287-Find-the-Duplicate-Number
loczek Jul 8, 2022
ba50ce0
Created 300-Longest-Increasing-Subsequence
loczek Jul 8, 2022
db7a2a8
Created 322-Coin-Change
loczek Jul 8, 2022
465e2e8
Created 338-Counting-Bits
loczek Jul 8, 2022
134af4e
Created 347-Top-K-Frequent-Elements
loczek Jul 8, 2022
7cf935f
Created 543-Diameter-of-Binary-Tree
loczek Jul 8, 2022
9cf30c6
Created 567-Permutation-in-String
loczek Jul 8, 2022
2a612a0
Created 572-Subtree-of-Another-Tree
loczek Jul 8, 2022
9e78942
Created 647-Palindromic-Substrings
loczek Jul 8, 2022
9f0a84f
Created 704-Binary-Search
loczek Jul 8, 2022
6c457b7
Created 746-Min-Cost-Climbing-Stairs
loczek Jul 8, 2022
f1495f0
Created 853-Car-Fleet
loczek Jul 8, 2022
c525834
Created 875-Koko-Eating-Bananas
loczek Jul 8, 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
28 changes: 28 additions & 0 deletions typescript/100-Same-Tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
if (p === null && q === null) return true;

if ((p === null && q !== null) || (p !== null && q === null)) return false;

let leftSame = isSameTree(p.left, q.left);
let rightSame = isSameTree(p.right, q.right);

if (p.val === q.val && leftSame && rightSame) {
return true;
}

return false;
}
36 changes: 36 additions & 0 deletions typescript/102-Binary-Tree-Level-Order-Traversal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function levelOrder(root: TreeNode | null): number[][] {
const levels: number[][] = [];

function getHeight(node: TreeNode | null, height: number) {
if (!node) return 0;

if (node.left || node.right) {
getHeight(node.left, height + 1);
getHeight(node.right, height + 1);
}

if (levels[height]) {
levels[height].push(node.val);
} else {
levels[height] = [node.val];
}
}

getHeight(root, 0);

return levels;
}
19 changes: 19 additions & 0 deletions typescript/104-Maximum-Depth-of-Binary-Tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function maxDepth(root: TreeNode | null): number {
if (!root) return 0;

return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
19 changes: 19 additions & 0 deletions typescript/11-Container-With-Most-Water.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function maxArea(height: number[]): number {
let l = 0;
let r = height.length - 1;
let max = 0;

while (l < r) {
let area = (r - l) * Math.min(height[l], height[r]);

max = Math.max(max, area);

if (height[l] < height[r]) {
l += 1;
} else {
r -= 1;
}
}

return max;
}
29 changes: 29 additions & 0 deletions typescript/110-Balanced-Binary-Tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function isBalanced(root: TreeNode | null): boolean {
let array = getHeight(root);
return array[0];
}

function getHeight(root: TreeNode | null) {
if (!root) return [true, 0];

let [leftBalanced, leftHeight] = getHeight(root.left);
let [rightBalanced, rightHeight] = getHeight(root.right);

let balanced = leftBalanced && rightBalanced && Math.abs(rightHeight - leftHeight) <= 1;

return [balanced, 1 + Math.max(leftHeight, rightHeight)];
}
19 changes: 19 additions & 0 deletions typescript/121-Best-Time-To-Buy-and-Sell-Stock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function maxProfit(prices: number[]): number {
let max = 0;
let l = 0;
let r = 1;

while (r < prices.length) {
if (prices[l] < prices[r]) {
let profit = prices[r] - prices[l];
if (profit > max) {
max = profit;
}
} else {
l = r;
}
r++;
}

return max;
}
13 changes: 13 additions & 0 deletions typescript/125-Valid-Palindrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function isPalindrome(s: string): boolean {
const array = s.toLowerCase().replace(/[^A-Za-z0-9]/g, "").replace(/\s/g, "").split("");

for (let i = 0; i < array.length; i++) {
const first = array[i];
const second = array[array.length - 1 - i];

if (first !== second) {
return false;
}
}
return true;
}
16 changes: 16 additions & 0 deletions typescript/128-Longest-Consecutive-Sequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function longestConsecutive(nums: number[]): number {
const number = new Set(nums);
let longest = 0;

for (const n of nums) {
if (!number.has(n - 1)) {
let length = 0;
while (number.has(n + length)) {
length += 1;
}
longest = Math.max(length, longest);
}
}

return longest;
}
33 changes: 33 additions & 0 deletions typescript/131-Palindrome-Partitioning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function partition(s: string): string[][] {
let res: string[][] = [];
let part: string[] = [];

function dfs(i: number) {
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: string, l: number, r: number) {
while (l < r) {
if (s[l] != s[r]) {
return false;
}
l = l + 1;
r = r - 1;
}
return true;
}
}
9 changes: 9 additions & 0 deletions typescript/136-Single-Number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function singleNumber(nums: number[]): number {
let res = 0;

for (let i = 0; i < nums.length; i++) {
res = nums[i] ^ res;
}

return res;
}
18 changes: 18 additions & 0 deletions typescript/139-Word-Break.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function wordBreak(s: string, wordDict: string[]): boolean {
const dp = Array(s.length + 1).fill(false);

dp[s.length] = true;

for (let i = s.length - 1; i > -1; i--) {
for (const w of wordDict) {
if (i + w.length <= s.length && s.slice(i, i + w.length) == w) {
dp[i] = dp[i + w.length];
}
if (dp[i]) {
break;
}
}
}

return dp[0];
}
28 changes: 28 additions & 0 deletions typescript/141-Linked-List-Cycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function hasCycle(head: ListNode | null): boolean {
let slow = head;
let fast = head;

while (fast && fast.next) {
if (slow) {
slow = slow.next;
}
fast = fast.next.next;
if (slow == fast) {
return true;
}
}

return false;
}
48 changes: 48 additions & 0 deletions typescript/143-Reorder-List.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

/**
Do not return anything, modify head in-place instead.
*/
function reorderList(head: ListNode | null): void {
let slow: ListNode | null | undefined = head;
let fast: ListNode | null | undefined = head?.next;

while (fast && fast.next) {
slow = slow?.next;
fast = fast?.next?.next;
}

// reverse second half
if (!(slow && slow.next)) return;
let second: ListNode | null = slow.next;
slow.next = null;
let prev: ListNode | null = null;
while (second) {
let temp = second.next;
second.next = prev;
prev = second;
second = temp;
}

// merge two halfs
let first: any = head;
second = prev;
while (second) {
let temp1 = first.next;
let temp2 = second.next;
first.next = second;
second.next = temp1;
first = temp1;
second = temp2;
}
}
29 changes: 29 additions & 0 deletions typescript/15-3Sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function threeSum(nums: number[]): number[][] {
const res: number[][] = [];
nums = nums.sort((a, b) => a - b);

for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}

let l = i + 1;
let r = nums.length - 1;
while (l < r) {
let sum = nums[i] + nums[l] + nums[r];

if (sum > 0) {
r -= 1;
} else if (sum < 0) {
l += 1;
} else {
res.push([nums[i], nums[l], nums[r]]);
l += 1;
while (nums[l] == nums[l - 1] && l < r) {
l += 1;
}
}
}
}
return res;
}
23 changes: 23 additions & 0 deletions typescript/150-Evaluate-Reverse-Polish-Notation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function evalRPN(tokens: string[]): number {
const stack: number[] = [];

tokens.forEach((token) => {
if (token === "+") {
stack.push(Number(stack.pop()) + Number(stack.pop()));
} else if (token === "-") {
const a = Number(stack.pop());
const b = Number(stack.pop());
stack.push(b - a);
} else if (token === "/") {
const a = Number(stack.pop());
const b = Number(stack.pop());
stack.push(Math.trunc(b / a));
} else if (token === "*") {
stack.push(Number(stack.pop()) * Number(stack.pop()));
} else {
stack.push(Number(token));
}
});

return stack[0];
}
Loading