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
| const arr = [ | |
| { time: 1, value: 4}, | |
| { time: 240, value: 1 }, | |
| { time: 760 , value: 3 }, | |
| { time: 5650, value: 1 }, | |
| { time: 7050, value: 17 }, | |
| { time: 7150, value: 16 }, | |
| ]; | |
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
| function searchInsert(nums: number[], target: number): number { | |
| let hi = nums.length-1; | |
| let lo = 0; | |
| while(lo <= hi) { | |
| let mid = Math.round((hi + lo) / 2); | |
| if(nums[mid] > target) { | |
| hi = mid - 1; | |
| } | |
| else if(nums[mid] < target) { |
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
| function plusOne(digits: number[]): number[] { | |
| for(let i = digits.length - 1; i >= 0; i--) { | |
| if(digits[i] !== 9) { | |
| digits[i]++; | |
| break; | |
| } else if(i === 0) { | |
| digits[i] = 0; | |
| digits.unshift(1); | |
| } else { | |
| digits[i] = 0; |
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
| function addBinary(a: string, b: string): string { | |
| return (BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2); | |
| }; | |
| console.log(addBinary("11","1")); // output: 100 | |
| console.log(addBinary("1010","1011")); // output: 10101 |
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
| function mySqrt(x: number): number { | |
| let temp = 0; | |
| let sqrt = x / 2; | |
| while(temp !== sqrt) { | |
| temp = sqrt; | |
| sqrt = (x / temp + temp) / 2; | |
| } | |
| return sqrt; |
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
| /** | |
| * 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) | |
| * } | |
| * } |
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
| 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 deleteDuplicates(head: ListNode | null): ListNode | null { | |
| if(head === null || head.next === null) return head; |
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
| // a.k.a famous fibbonaci | |
| function climbStairs(n: number): number { | |
| if (n <= 1) { | |
| return 1; | |
| } | |
| return climbStairs(n - 1) + climbStairs(n - 2); | |
| }; | |
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
| /** | |
| Do not return anything, modify nums1 in-place instead. | |
| */ | |
| function merge(nums1: number[], m: number, nums2: number[], n: number): void { | |
| while (n) { | |
| if (nums1[m - 1] > nums2[n - 1]) { | |
| nums1[m + n - 1] = nums1[--m]; | |
| } else { | |
| nums1[m + n - 1] = nums2[--n]; | |
| } |
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
| 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) | |
| } | |
| } |