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
| /* Preorder traversal | |
| The idea is to do a preorder traversal of the tree. | |
| In the preorder traversal, keep track of the value calculated till the current node, let this value be val. | |
| For every node, we update the val as val10 plus node’s data. | |
| */ | |
| var sumNumberUtil = function(node, val) { | |
| if (!node) { | |
| return 0; | |
| } | |
| 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
| /** | |
| * @param {number} x | |
| * @param {number} y | |
| * @return {number} | |
| */ | |
| var hammingDistance = function(x, y) { | |
| var result = 0; | |
| var xbits = x.toString(2); | |
| var ybits = y.toString(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
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| // Bit manipulation. Fastest. | |
| var missingNumber = function(nums) { | |
| var xor = 0; | |
| var i = 0; | |
| for (i=0; i<nums.length; i++) { |
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 isBadVersion() | |
| * | |
| * @param {integer} version number | |
| * @return {boolean} whether the version is bad | |
| * isBadVersion = function(version) { | |
| * ... | |
| * }; | |
| */ |
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
| /** | |
| * @param {string} s | |
| * @param {string} t | |
| * @return {boolean} | |
| */ | |
| var isIsomorphic = function(s, t) { | |
| var result = true; | |
| if (s.length === t.length) { | |
| var hash1 = []; |
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 factorial = function(n) { | |
| var result = 1; | |
| for (var i=n; i>0; i--) { | |
| result *= i; | |
| } | |
| 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
| /** | |
| * @param {string} s | |
| * @param {string} p | |
| * @return {number[]} | |
| */ | |
| var findAnagrams = function(s, p) { | |
| var result = []; | |
| if (p.length <= s.length) { | |
| // Build a hash of the letters. |
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 a binary tree node. | |
| * function TreeNode(val) { | |
| * this.val = val; | |
| * this.left = this.right = null; | |
| * } | |
| */ | |
| /** | |
| * @param {TreeNode} root | |
| * @param {number} 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
| var mathManager = { | |
| findProduct: function(product, arr) { | |
| var result = null; | |
| for (var i=0; i<arr.length; i++) { | |
| for (var j=i+1; j<arr.length; j++) { | |
| if (product === arr[i] * arr[j]) { | |
| result = [arr[i], arr[j]]; | |
| break; | |
| } |
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 mergeSort(arr) { | |
| var result = []; | |
| // Calculate mid point to split array. | |
| var mid = Math.floor(arr.length / 2); | |
| var left = arr.slice(0, mid); | |
| var right = arr.slice(mid, arr.length); | |
| // If more than one element, split the array again, until we get to a single element (implicitly sorted). | |
| if (left.length > 1) { |