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
| // Approach 1: Check the first and last digit in each row to see if the target is inside it. If so, search the row for the target. | |
| /** | |
| * @param {number[][]} matrix | |
| * @param {number} target | |
| * @return {boolean} | |
| */ | |
| var searchMatrix = function(matrix, target) { | |
| var result = false; | |
| var height = matrix.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
| // Recursive style 1. | |
| function factorial(n) { | |
| var result = n > 1 ? n : 1; | |
| if (n > 1) { | |
| result *= factorial(n - 1); | |
| } | |
| 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} t | |
| * @return {boolean} | |
| */ | |
| var isAnagram = function(s, t) { | |
| var result = true; | |
| if (s.length < t.length || t.length < s.length) { | |
| result = 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
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var findMaxConsecutiveOnes = function(nums) { | |
| var maxCount = 0; | |
| var count = 0; | |
| for (var i in nums) { | |
| count = nums[i] === 1 ? count + 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 {number[]} nums | |
| * @param {number} target | |
| * @return {boolean} | |
| */ | |
| var search = function(nums, target) { | |
| var result = false; | |
| var pivot = false; | |
| var previous = 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
| /** | |
| * @param {number[]} nums | |
| * @param {number} target | |
| * @return {boolean} | |
| */ | |
| var search = function(nums, target) { | |
| var result = -1; | |
| var pivot = false; | |
| var previous = 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
| var addTwoNumbers = function(l1, l2) { | |
| var value1 = 0; | |
| var value2 = 0; | |
| var digits1 = []; | |
| var digits2 = []; | |
| var item = l1; | |
| while (item) { | |
| digits1.push(item.val); |
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} n | |
| * @return {string[]} | |
| */ | |
| var fizzBuzz = function(n) { | |
| var result = []; | |
| for (var i=1; i<=n; i++) { | |
| if (i % 3 === 0 && i % 5 === 0) { | |
| result.push('FizzBuzz'); |
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
| // Returns array. | |
| var insertionSortList = function(head) { | |
| var result = []; | |
| var item = head; | |
| while (item) { | |
| if (!result.length) { | |
| result.push(item.val); | |
| } | |
| 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 {string} version1 | |
| * @param {string} version2 | |
| * @return {number} | |
| */ | |
| var compareVersion = function(version1, version2) { | |
| var result = 0; | |
| // Split version number parts. | |
| var parts1 = version1.split('.').map(function(i) { return parseInt(i); }); |