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 Relationship { | |
| constructor(root) { | |
| this.relObj = {} | |
| this.iterate(root, null, 0) | |
| } | |
| iterate(root, parent, level) { | |
| if (root) { | |
| this.relObj[root.val] = { level: level, parent: parent } | |
| this.iterate(root.left, root, level + 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
| class Stack { | |
| constructor(root) { | |
| this.stack = [] | |
| while (root) { | |
| this.stack.push(root) | |
| root = root.left | |
| } | |
| } | |
| hasNext() { |
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 hashMap = {} | |
| function countNumberOfWays (steps) { | |
| if (steps <= 0) { | |
| return 0 | |
| } else if (steps === 1) { | |
| return 1 | |
| } else if (steps === 2) { | |
| return 2 | |
| } else { |
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 findLengthOfLongestSubString(str) { | |
| var start = end = lengthOfLongestSubString = 0 | |
| var hashMap = {} | |
| while(end < str.length) { | |
| if (str[end] in hashMap) { | |
| start = hashMap[str[end]] + 1 | |
| } else { | |
| lengthOfLongestSubString = Math.max(lengthOfLongestSubString, (end-start+1)) | |
| } | |
| hashMap[str[end]] = end |
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 initializeMap(i, j) { | |
| var result = new Array(i) | |
| for (var x = 0; x < i; x++) { | |
| var arr = new Array(j) | |
| arr.fill(null) | |
| result[x] = arr | |
| } | |
| return result | |
| } |
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
| var leftArray = rightArray = prod = [] | |
| var leftHashMap = rightHashMap = {} | |
| function constructLeftArrayElement (arr, i) { | |
| if (i <= 0) { | |
| leftHashMap[i] = 1 | |
| return 1 | |
| } else { | |
| if (leftHashMap.hasOwnProperty(i-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 swap(arr, i, j) { | |
| var temp = arr[i] | |
| arr[i] = arr[j] | |
| arr[j] = temp | |
| return arr | |
| } | |
| function partition(arr, start, end) { | |
| var pivot = end | |
| while(start <= end) { |
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 swap(arr, i, j) { | |
| var temp = arr[i] | |
| arr[i] = arr[j] | |
| arr[j] = temp | |
| return arr | |
| } | |
| function partition(arr, start, end) { | |
| var pivot = end | |
| while(start <= end) { |
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 swap(arr, i, j) { | |
| var temp = arr[i] | |
| arr[i] = arr[j] | |
| arr[j] = temp | |
| return arr | |
| } | |
| function partition(arr, start, end) { | |
| var pivot = end | |
| while(start <= end) { |
NewerOlder