├── 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 |
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
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 |2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
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 | Given a string s
, find the length of the longest substring without repeating characters.
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 |0 <= s.length <= 5 * 104
s
consists of English letters, digits, symbols and spaces.