Skip to content

Commit 9dbe915

Browse files
committed
add english version and new theme
1 parent 5273784 commit 9dbe915

864 files changed

Lines changed: 80264 additions & 61 deletions

File tree

Some content is hidden

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

‎website/config.toml‎

Lines changed: 66 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,78 @@ enableGitInfo = true
99
# Book configuration
1010
disablePathToLower = true
1111

12+
# 多语言默认语言(必须放在任何 [table] 之前,否则会被误嵌套)。
13+
# 英文为默认;英文在 /en/、中文在 /zh/,根路径 / 自动跳转到默认语言 /en/,
14+
# 这样英文既是默认、又保留独立的 /en/ 入口(与 /zh/ 对称)。
15+
defaultContentLanguage = "en"
16+
defaultContentLanguageInSubdir = true
17+
1218
# Needed for mermaid/katex shortcodes
1319
[markup]
1420
[markup.goldmark.renderer]
1521
unsafe = true
1622

1723
[markup.tableOfContents]
18-
startLevel = 1
19-
20-
# Multi-lingual mode config
21-
# There are different options to translate files
22-
# See https://gohugo.io/content-management/multilingual/#translation-by-filename
23-
# And https://gohugo.io/content-management/multilingual/#translation-by-content-directory
24-
#[languages]
25-
#[languages.en]
26-
# languageName = 'English'
27-
# contentDir = 'content'
28-
# weight = 1
29-
30-
#[languages.ru]
31-
# languageName = 'Russian'
32-
# contentDir = 'content.ru'
33-
# weight = 2
34-
35-
#[languages.cn]
36-
# languageName = 'Chinese'
37-
# contentDir = 'content.cn'
38-
# weight = 3
39-
40-
[menu]
41-
[[menu.before]]
42-
name = "Blog"
43-
url = "https://halfrost.com"
44-
weight = 10
45-
[[menu.before]]
46-
name = "Github"
47-
url = "https://github.com/halfrost"
48-
weight = 20
49-
[[menu.before]]
50-
name = "WeChat Official Accounts"
51-
url = "https://img.halfrost.com/wechat-qr-code.png"
52-
weight = 30
53-
[[menu.before]]
54-
name = "Twitter"
55-
url = "https://twitter.com/halffrost"
56-
weight = 40
57-
[[menu.before]]
58-
name = "Weibo"
59-
url = "https://weibo.com/halfrost"
60-
weight = 50
24+
# 从 2 级标题开始收录,右侧目录不再收录很长的 H1 题目标题(英文标题很长会撑出右边栏)
25+
startLevel = 2
26+
27+
# Multi-lingual mode: 中文(默认, 根路径 /) + English(/en/),两套都预渲染成静态页面,
28+
# 由主题右下角的语言下拉一键切换(footer 里 IsMultiLingual 时显示)。
29+
# 中文内容在 content/,英文内容在 content.en/(由翻译脚本生成)。
30+
[languages]
31+
[languages.zh]
32+
languageName = "中文"
33+
languageCode = "zh-CN"
34+
contentDir = "content"
35+
weight = 2
36+
[languages.zh.menu]
37+
[[languages.zh.menu.before]]
38+
name = "Blog"
39+
url = "https://halfrost.com"
40+
weight = 10
41+
[[languages.zh.menu.before]]
42+
name = "Github"
43+
url = "https://github.com/halfrost"
44+
weight = 20
45+
[[languages.zh.menu.before]]
46+
name = "WeChat Official Accounts"
47+
url = "https://img.halfrost.com/wechat-qr-code.png"
48+
weight = 30
49+
[[languages.zh.menu.before]]
50+
name = "Twitter"
51+
url = "https://twitter.com/halffrost"
52+
weight = 40
53+
[[languages.zh.menu.before]]
54+
name = "Weibo"
55+
url = "https://weibo.com/halfrost"
56+
weight = 50
57+
58+
[languages.en]
59+
languageName = "English"
60+
languageCode = "en-US"
61+
contentDir = "content.en"
62+
weight = 1
63+
[languages.en.menu]
64+
[[languages.en.menu.before]]
65+
name = "Blog"
66+
url = "https://halfrost.com"
67+
weight = 10
68+
[[languages.en.menu.before]]
69+
name = "Github"
70+
url = "https://github.com/halfrost"
71+
weight = 20
72+
[[languages.en.menu.before]]
73+
name = "WeChat Official Accounts"
74+
url = "https://img.halfrost.com/wechat-qr-code.png"
75+
weight = 30
76+
[[languages.en.menu.before]]
77+
name = "Twitter"
78+
url = "https://twitter.com/halffrost"
79+
weight = 40
80+
[[languages.en.menu.before]]
81+
name = "Weibo"
82+
url = "https://weibo.com/halfrost"
83+
weight = 50
6184
#[[menu.after]]
6285
# name = "Github"
6386
# url = "https://github.com/alex-shpak/hugo-book"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# [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.
31+
32+
## Code
33+
34+
```go
35+
36+
package leetcode
37+
38+
func twoSum(nums []int, target int) []int {
39+
m := make(map[int]int)
40+
for i := 0; i < len(nums); i++ {
41+
another := target - nums[i]
42+
if index, ok := m[another]; ok {
43+
return []int{index, i}
44+
}
45+
m[nums[i]] = i
46+
}
47+
return nil
48+
}
49+
50+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# [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.
39+
40+
41+
## Code
42+
43+
```go
44+
45+
package leetcode
46+
47+
/**
48+
* Definition for singly-linked list.
49+
* type ListNode struct {
50+
* Val int
51+
* Next *ListNode
52+
* }
53+
*/
54+
55+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
56+
head := &ListNode{Val: 0}
57+
n1, n2, carry, current := 0, 0, 0, head
58+
for l1 != nil || l2 != nil || carry != 0 {
59+
if l1 == nil {
60+
n1 = 0
61+
} else {
62+
n1 = l1.Val
63+
l1 = l1.Next
64+
}
65+
if l2 == nil {
66+
n2 = 0
67+
} else {
68+
n2 = l2.Val
69+
l2 = l2.Next
70+
}
71+
current.Next = &ListNode{Val: (n1 + n2 + carry) % 10}
72+
current = current.Next
73+
carry = (n1 + n2 + carry) / 10
74+
}
75+
return head.Next
76+
}
77+
78+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [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+
func lengthOfLongestSubstring(s string) int {
60+
if len(s) == 0 {
61+
return 0
62+
}
63+
var bitSet [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+
func lengthOfLongestSubstring1(s string) int {
86+
if len(s) == 0 {
87+
return 0
88+
}
89+
var freq [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+
func lengthOfLongestSubstring2(s string) int {
108+
right, left, res := 0, 0, 0
109+
indexes := make(map[byte]int, len(s))
110+
for left < len(s) {
111+
if idx, ok := indexes[s[left]]; ok && idx >= right {
112+
right = idx + 1
113+
}
114+
indexes[s[left]] = left
115+
left++
116+
res = max(res, left-right)
117+
}
118+
return res
119+
}
120+
121+
func max(a int, b int) int {
122+
if a > b {
123+
return a
124+
}
125+
return b
126+
}
127+
128+
```

0 commit comments

Comments
 (0)