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
| /* | |
| Insert Node at the end of a linked list | |
| head pointer input could be NULL as well for empty list | |
| Node is defined as | |
| class Node { | |
| int data; | |
| Node next; | |
| Node prev; | |
| } | |
| */ |
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
| /* Method: Traverse through half the string and compare the left-side letter with the right-side letter (hence, traversing only half the string). | |
| If the left letter does not equal the right letter, we have found the location of the removal letter to form a palindrome. | |
| To determine which side to remove, check if the next left-side letter (i+1) matches the right-side AND we result in a complete palindrome. | |
| Otherwise, check if the next right-side letter (str.length - 2 - j) matches the left-side AND we result in a complete palindrome. | |
| */ | |
| function isPalindrome(str) { | |
| var result = true; | |
| for (var i=0; i<str.length / 2; 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
| process.stdin.resume(); | |
| process.stdin.setEncoding('ascii'); | |
| var input_stdin = ""; | |
| var input_stdin_array = ""; | |
| var input_currentline = 0; | |
| process.stdin.on('data', function (data) { | |
| input_stdin += data; | |
| }); |
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
| /* Method 1: Step through each linked list. When the pointer reaches the end of the list, set it to the head of the other list. | |
| The pointers will eventually collide. Runtime O(n), Space O(1) | |
| */ | |
| /* | |
| Insert Node at the end of a linked list | |
| head pointer input could be NULL as well for empty list | |
| Node is defined as | |
| class Node { | |
| int data; |
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
| /* Method: Solution uses a hash of each word and its assigned pattern symbol, | |
| along with a second hash to record pattern symbols that have already been used. | |
| */ | |
| /** | |
| * @param {string} pattern | |
| * @param {string} str | |
| * @return {boolean} | |
| */ | |
| var wordPattern = function(pattern, str) { |
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
| /* Method: Works by using a map of symbol values and walking backwards in the string checking the current symbol with the one to the right of it. | |
| If the symbol to the right is greater, then we subtract the current symbol (ie., IV IX etc). | |
| Otherwise, we just add the current symbol. | |
| See also http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm | |
| */ | |
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var romanToInt = function(s) { |
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 isUgly = function(num) { | |
| // Keep dividing by 2, 3, 5 until we arrive at 1. | |
| var arr = [2, 3, 5]; | |
| arr.forEach(function(prime) { | |
| while (num && num % prime === 0) { | |
| num /= prime; | |
| } | |
| }); | |
| return num === 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
| /* This solution goes through each house, finds its closest left and right heater, and then gets the distance for the closest heater. | |
| If the closest heater is the new maximum range, we keep it. Otherwise, we keep the prior maximum range found. | |
| If a house is to the left of all heaters, we use the distance between the house and heaters[0]. | |
| If a house is to the right of all heaters, we use the distance between the house and heaters[heaters.length - 1]. | |
| */ | |
| var findRadius = function(houses, heaters) { | |
| var result = 0; | |
| if (houses && houses.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
| /* Method 1: Using hashCode() to store previously seen nodes. */ | |
| /* | |
| Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty. | |
| A Node is defined as: | |
| class Node { | |
| int data; | |
| Node next; | |
| } |
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
| /* | |
| Node is defined as | |
| class Node { | |
| int data; | |
| Node next; | |
| } | |
| */ | |
| Node RemoveDuplicates(Node head) { | |
| Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>(); |