Skip to content

Commit bb04aa4

Browse files
6borisgitbook-bot
authored andcommitted
GitBook: [master] 188 pages modified
1 parent 170a229 commit bb04aa4

File tree

181 files changed

+10479
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+10479
-182
lines changed

SUMMARY.md

+180-182
Large diffs are not rendered by default.

docs/leetcode/1-100/0001.two-sum.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 0001.Two-Sum
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# 0002.Add-Two-Numbers
2+
3+
## Description
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+
```text
12+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 0 -> 8
14+
Explanation: 342 + 465 = 807.
15+
```
16+
17+
**Tags:** Linked List, Math
18+
19+
## 题意
20+
21+
> 以链表表示一个数,低位在前,高位在后,所以题中的例子就是 342 + 465 = 807.
22+
23+
## 题解
24+
25+
### 思路1
26+
27+
> 直接模拟计算就好
28+
29+
```go
30+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
31+
node := &ListNode{Val: 0, Next: nil}
32+
n1, n2, tmp := l1, l2, node
33+
sum := 0
34+
35+
for n1 != nil || n2 != nil {
36+
sum /= 10
37+
if n1 != nil {
38+
sum += n1.Val
39+
n1 = n1.Next
40+
41+
}
42+
if n2 != nil {
43+
sum += n2.Val
44+
n2 = n2.Next
45+
}
46+
tmp.Next = &ListNode{Val: sum % 10}
47+
tmp = tmp.Next
48+
}
49+
if sum/10 != 0 {
50+
tmp.Next = &ListNode{Val: 1}
51+
}
52+
return node.Next
53+
}
54+
```
55+
56+
### 思路2
57+
58+
> 思路2 \`\`\`go
59+
60+
\`\`\`
61+
62+
## 结语
63+
64+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode](https://github.com/kylesliu/awesome-golang-algorithm)
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 0003.Longest-Substring-Without-Repeating-Characters
2+
3+
## Description
4+
5+
## Description
6+
7+
Given a string, find the length of the **longest substring** without repeating characters.
8+
9+
**Examples:**
10+
11+
Given `"abcabcbb"`, the answer is `"abc"`, which the length is 3.
12+
13+
Given `"bbbbb"`, the answer is `"b"`, with the length of 1.
14+
15+
Given `"pwwkew"`, the answer is `"wke"`, with the length of 3. Note that the answer must be a **substring**, `"pwke"` is a _subsequence_ and not a substring.
16+
17+
**Tags:** Hash Table, Two Pointers, String
18+
19+
## 题意
20+
21+
> 计算不带重复字符的最长子字符串的长度
22+
23+
## 题解
24+
25+
### 思路1
26+
27+
> 开辟一个 hash 数组来存储该字符上次出现的位置,比如 `hash[a] = 3` 就是代表 `a` 字符前一次出现的索引在 3,遍历该字符串,获取到上次出现的最大索引(只能向前,不能退后),与当前的索引做差获取的就是本次所需长度,从中迭代出最大值就是最终答案。
28+
29+
```go
30+
// O(n) time O(1) space Solution
31+
func lengthOfLongestSubstring(s string) int {
32+
var chPosition [256]int // [0, 0, 0, ...]
33+
maxLength, substringLen, lastRepeatPos := 0, 0, 0
34+
35+
for i := 0; i < len(s); i++ {
36+
if pos := chPosition[s[i]]; pos > 0 {
37+
// record current substring length
38+
maxLength = Max(substringLen, maxLength)
39+
40+
// update characters position
41+
chPosition[s[i]] = i + 1
42+
43+
// update last repeat character position
44+
lastRepeatPos = Max(pos, lastRepeatPos)
45+
46+
// update the current substring from last repeat character
47+
substringLen = i + 1 - lastRepeatPos
48+
} else {
49+
substringLen += 1
50+
chPosition[s[i]] = i + 1
51+
}
52+
}
53+
54+
return Max(maxLength, substringLen)
55+
}
56+
func Max(x, y int) int {
57+
if x > y {
58+
return x
59+
}
60+
return y
61+
}
62+
```
63+
64+
### 思路2
65+
66+
> 暴力循环
67+
>
68+
> \`\`\`go // 暴力求解\(会超时\) func lengthOfLongestSubstring2\(s string\) int { ans := 0
69+
70+
```text
71+
for i := 0; i < len(s); i++ {
72+
for j := i + 1; j <= len(s); j++ {
73+
if allUnique(s, i, j) {
74+
ans = Max(ans, j-i)
75+
}
76+
}
77+
}
78+
return ans
79+
```
80+
81+
}
82+
83+
func allUnique\(s string, start int, end int\) bool { sMap := make\(map\[string\]int\)
84+
85+
```text
86+
for i := start; i < end; i++ {
87+
if sMap[string(s[i])] > 0 {
88+
return false
89+
}
90+
sMap[string(s[i])]++
91+
}
92+
93+
return true
94+
```
95+
96+
}
97+
98+
func Max\(x, y int\) int { if x &gt; y { return x } return y }
99+
100+
\`\`\`
101+
102+
## Benchmark
103+
104+
> 测试了一下2中方法基本上有10000倍的时间差距
105+
>
106+
> ![](https://github.com/kylesliu/awesome-golang-algorithm/blob/master/assets/images/0003-BenchMark.png)
107+
108+
## 结语
109+
110+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode](https://github.com/kylesliu/awesome-golang-algorithm)
111+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# 0004.Median-of-Two-Sorted-Arrays
2+
3+
## Description
4+
5+
There are two sorted arrays nums1 and nums2 of size m and n respectively.
6+
7+
Find the median of the two sorted arrays. The overall run time complexity should be O\(log \(m+n\)\).
8+
9+
You may assume nums1 and nums2 cannot be both empty.
10+
11+
**Example 1:**
12+
13+
```text
14+
nums1 = [1, 3]
15+
nums2 = [2]
16+
17+
The median is 2.0
18+
```
19+
20+
**Example 2:**
21+
22+
```text
23+
nums1 = [1, 2]
24+
nums2 = [3, 4]
25+
26+
The median is (2 + 3)/2 = 2.5
27+
```
28+
29+
**Tags:** Math, String
30+
31+
## 题意
32+
33+
> 题意是在2个排序好的数组中寻找中位数
34+
35+
## 题解
36+
37+
### 思路1
38+
39+
> 一种是根据规律构建儿茶搜索树
40+
41+
```go
42+
// O(log(min(m, n))) Solution
43+
//
44+
solutions
45+
//
46+
// nums1: A1, A2, A3, ..., Am
47+
// nums2: B1, B2, B3, ..., Bn
48+
//
49+
// the key point is to find a i in [0, m] and a j in [0, n], which makes i + j = m + n + 1 / 2
50+
// so we get two sets of numbers as follow,
51+
// Left Part | Right Part
52+
// A1, A2, A3, ..., Ai | Ai+1, Ai+2, Ai+3, ..., Am
53+
// B1, B2, B3, ..., Bj | Bj+1, Bj+2, Bj+3, ..., Bn
54+
// if Ai <= Bj+1 and Bj <= Ai+1,
55+
// than we have two sets of numbers which any number in right part is greater than left part
56+
//
57+
// Left Part | Right Part
58+
// | Ai+1, Ai+2, Ai+3, ..., Am
59+
// B1, B2, B3, ..., Bj | Bj+1, Bj+2, Bj+3, ..., Bn
60+
// if i == 0 means A is the right part, in that case, j + 1 will be out of range
61+
//
62+
// Left Part | Right Part
63+
// A1, A2, A3, ..., Ai |
64+
// B1, B2, B3, ..., Bj | Bj+1, Bj+2, Bj+3, ..., Bn
65+
// if i == m means A is the left part, in that case, i + 1 will be out of range
66+
//
67+
// in that condition,
68+
// * if m + n is even
69+
// the median is: max(Ai, Bj) + min(Ai+1, Bj+1) / 2
70+
// * if m + n is odd
71+
// the median is: max(Ai, Bj)
72+
```
73+
74+
```go
75+
package Solution
76+
77+
func Min(a int, b int) int {
78+
if a > b {
79+
return b
80+
}
81+
return a
82+
}
83+
84+
func Max(a int, b int) int {
85+
if a > b {
86+
return a
87+
}
88+
return b
89+
}
90+
91+
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
92+
m, n := len(nums1), len(nums2)
93+
halfLen := (m + n + 1) / 2
94+
95+
if m == 0 || n == 0 {
96+
nums := append(nums1, nums2...)
97+
if len(nums)%2 == 1 {
98+
return float64(nums[halfLen-1])
99+
} else {
100+
return (float64(nums[halfLen-1]) + float64(nums[halfLen])) / 2.0
101+
}
102+
}
103+
104+
// make sure m < n, so j = halfLen - i is always greater than zero
105+
var A, B []int
106+
if m < n {
107+
A, B = nums1, nums2
108+
} else {
109+
m, n = n, m
110+
A, B = nums2, nums1
111+
}
112+
113+
// find i in [0, m]
114+
// especial, if i == 0 means A is the right part, if i == m means A is the left part
115+
iMin, iMax := 0, m
116+
for iMin <= iMax {
117+
i := (iMin + iMax) / 2
118+
j := halfLen - i
119+
120+
if i > 0 && j >= 0 && j < n && A[i-1] > B[j] {
121+
// Ai > Bj+1, i need be smaller
122+
iMax = i - 1
123+
} else if j > 0 && i >= 0 && i < m && B[j-1] > A[i] {
124+
// Bj > Ai+1, i need be greater
125+
iMin = i + 1
126+
} else {
127+
var leftPartMax, rightPartMin float64
128+
if i == 0 {
129+
leftPartMax = float64(B[j-1])
130+
} else if j == 0 {
131+
leftPartMax = float64(A[i-1])
132+
} else {
133+
leftPartMax = float64(Max(A[i-1], B[j-1]))
134+
}
135+
136+
if (m+n)%2 == 1 {
137+
// m + n is odd
138+
return leftPartMax
139+
}
140+
141+
if i == m {
142+
rightPartMin = float64(B[j])
143+
} else if j == n {
144+
rightPartMin = float64(A[i])
145+
} else {
146+
rightPartMin = float64(Min(A[i], B[j]))
147+
}
148+
149+
// m + n is even
150+
return (leftPartMax + rightPartMin) / 2.0
151+
}
152+
}
153+
154+
return -1.0
155+
}
156+
```
157+
158+
### 思路2
159+
160+
> 思路2 \`\`\`go
161+
162+
\`\`\`
163+
164+
## 结语
165+
166+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode](https://github.com/kylesliu/awesome-golang-algorithm)
167+

0 commit comments

Comments
 (0)