You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9dbe915
Browse filesBrowse the repository at this point in the historyBrowse files
# [1. Two Sum](https://leetcode.com/problems/two-sum/)
2
+
3
+
## Problem
4
+
5
+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6
+
7
+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
8
+
9
+
**Example**:
10
+
11
+
```
12
+
13
+
Given nums = [2, 7, 11, 15], target = 9,
14
+
15
+
Because nums[0] + nums[1] = 2 + 7 = 9,
16
+
return [0, 1]
17
+
18
+
```
19
+
20
+
21
+
22
+
## Problem Summary
23
+
24
+
Find two numbers in an array whose sum equals the given value, and return the indices of the two numbers in the array.
25
+
26
+
## Solution Approach
27
+
28
+
The optimal time complexity for this problem is O(n).
29
+
30
+
Scan the array in order. For each element, look in the map for the other half of the number that can combine with it to make the given value. If it is found, directly return the indices of the two numbers. If it is not found, store this number in the map and wait until the “other half” number is encountered, then retrieve it and return the result.
# [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)
2
+
3
+
## Problem
4
+
5
+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6
+
7
+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8
+
9
+
**Example**:
10
+
11
+
```
12
+
13
+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
14
+
Output: 7 -> 0 -> 8
15
+
Explanation: 342 + 465 = 807.
16
+
```
17
+
18
+
19
+
20
+
## Problem Summary
21
+
22
+
Given 2 linked lists in reverse order, add them starting from the lowest digit. The result should also be output in reverse order, and the return value is the head node of the reversed result linked list.
23
+
24
+
## Solution Approach
25
+
26
+
What needs attention is the various carry issues.
27
+
28
+
Extreme case, for example
29
+
30
+
```
31
+
32
+
Input: (9 -> 9 -> 9 -> 9 -> 9) + (1 -> )
33
+
Output: 0 -> 0 -> 0 -> 0 -> 0 -> 1
34
+
35
+
36
+
```
37
+
38
+
To make the handling uniform, you can first create a dummy head node. The Next of this dummy head node points to the real head, so the head does not need to be handled separately, and you can directly use a while loop. In addition, the condition for determining loop termination should not be p.Next != nil; otherwise, the last digit still needs extra calculation. The loop termination condition should be p != nil.
# [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
2
+
3
+
## Problem
4
+
5
+
Given a string, find the length of the longest substring without repeating characters.
6
+
7
+
8
+
9
+
**Example 1**:
10
+
11
+
```
12
+
13
+
Input: "abcabcbb"
14
+
Output: 3
15
+
Explanation: The answer is "abc", with the length of 3.
16
+
17
+
```
18
+
19
+
**Example 2**:
20
+
21
+
```
22
+
23
+
Input: "bbbbb"
24
+
Output: 1
25
+
Explanation: The answer is "b", with the length of 1.
26
+
27
+
```
28
+
29
+
**Example 3**:
30
+
31
+
```
32
+
33
+
Input: "pwwkew"
34
+
Output: 3
35
+
Explanation: The answer is "wke", with the length of 3.
36
+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
37
+
38
+
```
39
+
40
+
## Problem Summary
41
+
42
+
43
+
In a string, find the longest substring without repeating characters.
44
+
45
+
## Solution Approach
46
+
47
+
This problem is similar to problems 438, 3, 76, and 567. The idea used is "sliding window".
48
+
49
+
The right boundary of the sliding window keeps moving to the right. As long as there are no repeated characters, the window boundary continues to expand to the right. Once a repeated character appears, the left boundary needs to be shrunk until the repeated character has moved out of the left boundary, and then the right boundary of the sliding window continues to move. Repeat this process. Each movement requires calculating the current length and determining whether the maximum length needs to be updated. The final maximum value is the answer required by the problem.
50
+
51
+
52
+
## Code
53
+
54
+
```go
55
+
56
+
package leetcode
57
+
58
+
// Solution 1: Bitmap
59
+
funclengthOfLongestSubstring(sstring) int {
60
+
iflen(s) == 0 {
61
+
return0
62
+
}
63
+
varbitSet [256]bool
64
+
result, left, right:=0, 0, 0
65
+
for left < len(s) {
66
+
// If the bitSet corresponding to the right-side character is marked true, it means this character repeats at position X; the left side needs to move forward until X is marked false
67
+
if bitSet[s[right]] {
68
+
bitSet[s[left]] = false
69
+
left++
70
+
} else {
71
+
bitSet[s[right]] = true
72
+
right++
73
+
}
74
+
if result < right-left {
75
+
result = right - left
76
+
}
77
+
if left+result >= len(s) || right >= len(s) {
78
+
break
79
+
}
80
+
}
81
+
return result
82
+
}
83
+
84
+
// Solution 2: Sliding window
85
+
funclengthOfLongestSubstring1(sstring) int {
86
+
iflen(s) == 0 {
87
+
return0
88
+
}
89
+
varfreq [127]int
90
+
result, left, right:=0, 0, -1
91
+
92
+
for left < len(s) {
93
+
if right+1 < len(s) && freq[s[right+1]] == 0 {
94
+
freq[s[right+1]]++
95
+
right++
96
+
97
+
} else {
98
+
freq[s[left]]--
99
+
left++
100
+
}
101
+
result = max(result, right-left+1)
102
+
}
103
+
return result
104
+
}
105
+
106
+
// Solution 3: Sliding window - hash bucket
107
+
funclengthOfLongestSubstring2(sstring) int {
108
+
right, left, res:=0, 0, 0
109
+
indexes:=make(map[byte]int, len(s))
110
+
for left < len(s) {
111
+
ifidx, ok:= indexes[s[left]]; ok && idx >= right {
0 commit comments