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 | |
| * @param {number} val | |
| * @return {number} | |
| */ | |
| var removeElement = function(nums, val) { | |
| for (var i=nums.length - 1; i >= 0; i--) { | |
| if (nums[i] === val) { | |
| nums.splice(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 isPalindrome = function(x) { | |
| return x.toString() == x.toString().split("").reverse().join(""); | |
| }; | |
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var largestPalindrome = function(n) { | |
| var value = Math.pow(10, n) - 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
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var lengthOfLongestSubstring = function(s) { | |
| var max = 0; | |
| var str = ''; | |
| var i = 0; | |
| var cache = []; | |
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[][]} matrix | |
| * @return {void} Do not return anything, modify matrix in-place instead. | |
| */ | |
| var rotate = function(matrix) { | |
| // reverse the rows | |
| matrix = matrix.reverse(); | |
| // swap the symmetric elements | |
| for (var i = 0; i < matrix.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
| // See also http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/ | |
| /** | |
| * Definition for a binary tree node. | |
| * function TreeNode(val) { | |
| * this.val = val; | |
| * this.left = this.right = 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 getNthDigit(val, n) | |
| { | |
| var modVal = val % Math.pow(10,n);//Remove all digits larger than nth | |
| return Math.floor(modVal / Math.pow(10,n-1));//Remove all digits less than nth | |
| } | |
| /** | |
| * @param {number} x | |
| * @return {boolean} | |
| */ |
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
| // Combinations and permutations. | |
| // Counts can be verified at http://www.calculatorsoup.com/calculators/discretemathematics/permutations.php | |
| // noprotect | |
| var combinations = function(arr) { | |
| var result = []; | |
| for (var i=0; i<arr.length; i++) { | |
| var item1 = arr[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
| /* jshint esnext: true */ | |
| var Person = class { | |
| constructor(first, last) { | |
| this.first = first; | |
| this.last = last; | |
| } | |
| toString() { | |
| return this.first + ' ' + this.last; |
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 generate = function(n) { | |
| var result = ''; | |
| n += ''; | |
| var digit = n[0]; | |
| var count = 1; | |
| for (var i=1; i<n.length; i++) { | |
| var s = n[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
| function quickSort(arr) { | |
| var sorted = []; | |
| var left = []; | |
| var right = []; | |
| if (arr && arr.length > 1) { | |
| // 1. Select a random number. | |
| var mid = arr[0]; | |
| // 2. Place all numbers less in the left array, greater in the right array. |