Last active
February 9, 2020 08:18
-
-
Save wzhudev/fad2928b0ed050562c905582b989a435 to your computer and use it in GitHub Desktop.
LeetCode Solutions
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 twoSum = function(nums, target) { | |
| /** | |
| * Map number to its position. | |
| */ | |
| const map = { | |
| [nums[0]]: 0 | |
| } | |
| const l = nums.length | |
| for (let i = 1; i < l; i++) { | |
| const current = nums[i] | |
| const diff = target - current | |
| const dest = map[diff] | |
| if (dest !== undefined) { | |
| return [dest, i] | |
| } else { | |
| map[current] = 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
| /** | |
| * Definition for singly-linked list. | |
| * function ListNode(val) { | |
| * this.val = val; | |
| * this.next = null; | |
| * } | |
| */ | |
| /** | |
| * @param {ListNode} l1 | |
| * @param {ListNode} l2 | |
| * @return {ListNode} | |
| */ | |
| var addTwoNumbers = function(l1, l2) { | |
| let head = { val: undefined, next: null } | |
| let current = head | |
| let incremental = false | |
| // Merge two array. | |
| while (l1 || l2 || incremental) { | |
| const val1 = l1 ? l1.val : 0; | |
| const val2 = l2 ? l2.val : 0; | |
| const val = val1 + val2 + (incremental ? 1 : 0) | |
| incremental = val >= 10; | |
| current.next = { val: val % 10, next: null } | |
| current = current.next | |
| l1 = l1 ? l1.next : null; | |
| l2 = l2 ? l2.next : null; | |
| } | |
| return head.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
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var lengthOfLongestSubstring = function(s) { | |
| if (!s) return 0 | |
| // Letter and the last index | |
| const map = { | |
| [s[0]]: 0 | |
| } | |
| const l = s.length | |
| let max = 1 | |
| let headIndex = 0 | |
| let currentLength = 1 | |
| for (let i = 1; i < l; i++) { | |
| const currentLetter = s[i] | |
| const lastIndex = map[currentLetter] | |
| if (lastIndex !== undefined && lastIndex >= headIndex) { | |
| headIndex = lastIndex + 1 | |
| currentLength = i - headIndex + 1 | |
| map[currentLetter] = i | |
| } else { | |
| currentLength += 1 | |
| map[currentLetter] = i | |
| if (currentLength > max) { | |
| max = currentLength | |
| } | |
| } | |
| } | |
| return max | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment