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 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
| 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 removeElement(nums: number[], val: number): number { | |
| const output = []; | |
| let j =0; | |
| for(let i = 0; i < nums.length; i++) { | |
| if(nums[i] != val) { | |
| nums[j++] = nums[i]; | |
| } | |
| } | |
| return j; |
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: 7 }, | |
| { time: 7150, value: 8 }, | |
| ]; | |
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 mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { |
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 isValid(s: string): boolean { | |
| const stack = []; | |
| const bracketLookup = { | |
| '{': '}', | |
| '(': ')', | |
| '[': ']', | |
| }; | |
| for(const key of s) { | |
| if(Object.keys(bracketLookup).includes(key)) { |
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 longestCommonPrefix(strs: string[]): string { | |
| let prefix = '""'; | |
| if(strs.length === 0) return prefix; | |
| // sort alphabetically | |
| strs = strs.sort(); | |
| prefix = strs[0]; | |
| for(let i = 1; i < strs.length; i++) { | |
| while(strs[i].indexOf(prefix) !== 0) { | |
| prefix = prefix.slice(0, prefix.length - 1); |
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 isPalindrome(x: number): boolean { | |
| return x.toString().split('').reverse().join('') === x.toString() ? true : false; | |
| }; |
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 twoSum(nums: number[], target: number): number[] { | |
| const result = []; | |
| for (let i = 0; i < nums.length; i++) { | |
| for(let j = i + 1; j <= nums.length - 1; j++) { | |
| if(target - (nums[i] + nums[j]) === 0) { | |
| result.push(i); | |
| result.push(j); | |
| } | |
| } | |
| } |