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
| /* This solution works by using an array for placeholders of each positive and negative value up to a and b. | |
| We then count the number of items in each array and remove from the oppositie array to get the final value. | |
| If the negative array is larger, we multiple by -1. | |
| This solution works, but is slow. | |
| */ | |
| /** | |
| * @param {number} a | |
| * @param {number} b | |
| * @return {number} |
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 isPrime = function(n) { | |
| var result = true; | |
| if (n <= 1) { | |
| result = false; | |
| } | |
| else { | |
| // We only need to check up to the sqrt of the number. | |
| for (var i=2; i*i<=n; i++) { | |
| if (n % 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
| var findComplement = function(num) { | |
| var result = 0; | |
| // Convert number to binary string. | |
| var bits = num.toString(2); | |
| var complement = ''; | |
| // Create opposite binary string for complement. | |
| for (var i=0; i<bits.length; i++) { | |
| complement += bits[i] === '0' ? '1' : '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
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var titleToNumber = function(s) { | |
| var result = 0; | |
| for (var i=0; i<s.length; i++) { | |
| result *= 26; | |
| result += ((s.charCodeAt(i) - 'A'.charCodeAt(0)) + 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
| /* Start at root and check if p and q are less, then go left, otherwise go right. */ | |
| var lowestCommonAncestor = function(root, p, q) { | |
| if (root.val > p.val && root.val > q.val) { | |
| return lowestCommonAncestor(root.left, p, q); | |
| } | |
| else if (root.val < p.val && root.val < q.val) { | |
| return lowestCommonAncestor(root.right, p, q); | |
| } | |
| else { | |
| return root; |
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 removeAdjacent = function(n) { | |
| var result = 0; | |
| var str = n.toString(); | |
| var numbers = []; | |
| for (var i=0; i<str.length; i++) { | |
| if (i+1 < str.length) { | |
| var a = str[i]; | |
| var b = str[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
| var findCharacter = function(s, t) { | |
| var result; | |
| s = s.toLowerCase(); | |
| t = t.toLowerCase(); | |
| var hash1 = []; | |
| var hash2 = []; | |
| var length = Math.max(s.length, t.length); | |
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 getDigits = function(x) { | |
| var result = []; | |
| while (x) { | |
| result.push(x % 10); | |
| x = Math.floor(x / 10); | |
| } | |
| 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 smallestByReplace = function(n) { | |
| var digits = n.toString(); | |
| var numbers = []; | |
| for (var i=0; i<digits.length; i++) { | |
| if (i+1 < digits.length) { | |
| var a = digits[i]; | |
| var b = digits[i+1]; | |
| var max = Math.max(a, b); |
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} str | |
| * @return {boolean} | |
| */ | |
| var repeatedSubstringPattern = function(str) { | |
| var result = false; | |
| var sameCount = 1; | |
| for (var i=1; i<str.length; i++) { | |
| if (str[i] === str[0]) { |