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 increasingTriplet = function(nums) { | |
| var result = false; | |
| // Patient sort algorithm. | |
| var pile1 = 9007199254740992; | |
| var pile2 = 9007199254740992; | |
| for (var i=0; i<nums.length; i++) { | |
| if (nums[i] <= pile1) { | |
| // First lowest 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
| // noprotect | |
| var createTree = function(arr) { | |
| var head = null; | |
| for (var i=0; i<arr.length; i++) { | |
| if (!head) { | |
| head = { value: arr[i] }; | |
| } | |
| else { | |
| // Search for location to add node. |
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 singleNumber = function(nums) { | |
| var result = nums[0]; | |
| var memory1 = {}; | |
| var memory2 = {}; | |
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 reverseString = function(s) { | |
| var result = ''; | |
| for (var i=s.length - 1; i>=0; i--) { | |
| result += s[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
| /* Approach 1. Loop through string and store vowels on array, placeholders in string. Then loop through string again and replace in vowels in reverse order. */ | |
| var reverseVowels = function(s) { | |
| var result = ''; | |
| var string = []; | |
| var vowels = []; | |
| // Start from front of string to find consonants and vowels. | |
| for (var i=0; i<s.length; i++) { | |
| var orig = s[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
| /* Same as basic method, but includes a look-ahead to speed up performance. */ | |
| var strStr = function(haystack, needle) { | |
| var result = -1; | |
| if (!needle.length) { | |
| result = 0; | |
| } | |
| else if (needle.length) { | |
| for (var i=0; i<haystack.length - needle.length + 1; i++) { | |
| var nextStartingPos = 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 {number} | |
| */ | |
| var searchInsert = function(nums, target) { | |
| var start = 0; | |
| var end = nums.length - 1; | |
| var index = Math.floor((end - start) / 2) + start; | |
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 singly-linked list. | |
| * function ListNode(val) { | |
| * this.val = val; | |
| * this.next = null; | |
| * } | |
| */ | |
| /** | |
| * @param {ListNode} head | |
| * @return {ListNode} |
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 numDecodings = function(s) { | |
| if (!s.length || s[0] == '0') return 0; | |
| // r2: decode ways of s[i-2] , r1: decode ways of s[i-1] | |
| var r1 = 1, r2 = 1; | |
| for (var i = 1; i < s.length; i++) { | |
| // zero voids ways of the last because zero cannot be used separately | |
| if (s[i] == '0') r1 = 0; | |
| // possible two-digit letter, so new r1 is sum of both while new r2 is the old r1 |