├── 1-two-sum ├── README.md └── two-sum.js └── 3-longest-substring-without-repeating-characters ├── README.md └── longest-substring-without-repeating-characters.js /1-two-sum/README.md: -------------------------------------------------------------------------------- 1 |

Two Sum

Difficulty: Easy

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

2 | 3 |

You may assume that each input would have exactly one solution, and you may not use the same element twice.

4 | 5 |

You can return the answer in any order.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: nums = [2,7,11,15], target = 9
12 | Output: [0,1]
13 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: nums = [3,2,4], target = 6
20 | Output: [1,2]
21 | 
22 | 23 |

Example 3:

24 | 25 |
26 | Input: nums = [3,3], target = 6
27 | Output: [0,1]
28 | 
29 | 30 |

 

31 |

Constraints:

32 | 33 | 39 | 40 |

 

41 | Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity? -------------------------------------------------------------------------------- /1-two-sum/two-sum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var twoSum = function(nums, target) { 7 | let res = new Map(); 8 | for (let i = 0; i < nums.length; i ++) { 9 | const sub = target - nums[i]; 10 | if (res.has(sub)) return [res.get(sub), i]; 11 | res.set(nums[i], i); 12 | } 13 | return []; 14 | }; -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/README.md: -------------------------------------------------------------------------------- 1 |

Longest Substring Without Repeating Characters

Difficulty: Medium

Given a string s, find the length of the longest substring without repeating characters.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
 7 | Input: s = "abcabcbb"
 8 | Output: 3
 9 | Explanation: The answer is "abc", with the length of 3.
10 | 
11 | 12 |

Example 2:

13 | 14 |
15 | Input: s = "bbbbb"
16 | Output: 1
17 | Explanation: The answer is "b", with the length of 1.
18 | 
19 | 20 |

Example 3:

21 | 22 |
23 | Input: s = "pwwkew"
24 | Output: 3
25 | Explanation: The answer is "wke", with the length of 3.
26 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
27 | 
28 | 29 |

 

30 |

Constraints:

31 | 32 | 36 | -------------------------------------------------------------------------------- /3-longest-substring-without-repeating-characters/longest-substring-without-repeating-characters.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var lengthOfLongestSubstring = function (s) { 6 | let chatMap = new Map(); 7 | let left = 0; 8 | let maxLen = 0; 9 | 10 | for (let right = 0; right < s.length; right ++) { 11 | const currentChar = s[right]; 12 | if (chatMap.has(currentChar) && chatMap.get(currentChar) >= left) { 13 | left = chatMap.get(currentChar) + 1; 14 | } 15 | chatMap.set(currentChar, right); 16 | maxLen = Math.max(maxLen, right - left + 1); 17 | } 18 | return maxLen; 19 | }; --------------------------------------------------------------------------------