Skip to content

Commit 4e3a42e

Browse files
committed
Merge pull request halfrost#45 from halfrost/add_hugo
Optimized solution 3、39 & update README
2 parents a78d782 + eadefeb commit 4e3a42e

File tree

6 files changed

+69
-9
lines changed

6 files changed

+69
-9
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</p>
99

1010
<p align='center'>
11-
<a href="https://leetcode.com/halfrost/"><img src="https://img.shields.io/badge/@halfrost-9650-yellow.svg">
11+
<a href="https://leetcode.com/halfrost/"><img src="https://img.shields.io/badge/@halfrost-8751-yellow.svg">
1212
<img src="https://img.shields.io/badge/build-passing-brightgreen.svg">
1313
<img src="https://img.shields.io/badge/language-Golang-abcdef.svg">
1414
<a href="https://halfrost.com"><img src="https://img.shields.io/badge/Blog-Halfrost--Field-80d4f9.svg?style=flat"></a>
@@ -26,13 +26,15 @@
2626
</p>
2727

2828
<p align='center'>
29-
支持 Progressive Web Apps 的题解电子书《LeetCode Cookbook》在线阅读 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">地址</a>
29+
支持 Progressive Web Apps 的题解电子书《LeetCode Cookbook》 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">Online Reading</a>
3030
</p>
3131

3232
## Data Structures
3333

3434
> 标识了 ✅ 的专题是完成所有题目了的,没有标识的是还没有做完所有题目的
3535

36+
<a href="https://books.halfrost.com/leetcode/"><img src="./website/static/logo.png" alt="logo" height="550" align="right" /></a>
37+
3638
* [Array](#array)
3739
* [String](#string)
3840
* [Two Pointers ✅](#two-pointers)
@@ -53,7 +55,6 @@
5355
* [Segment Tree ✅](#segment-tree)
5456
* [Binary Indexed Tree ✅](#binary-indexed-tree)
5557

56-
5758
| 数据结构 | 变种 | 相关题目 |
5859
|:-------:|:-------|:------|
5960
|顺序线性表:向量|||

leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
package leetcode
22

3+
// 解法一 位图
34
func lengthOfLongestSubstring(s string) int {
5+
if len(s) == 0 {
6+
return 0
7+
}
8+
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
9+
var bitSet [256]uint8
10+
result, left, right := 0, 0, 0
11+
for left < len(s) {
12+
if right < len(s) && bitSet[s[right]] == 0 {
13+
// 标记对应的 ASCII 码为 1
14+
bitSet[s[right]] = 1
15+
right++
16+
} else {
17+
// 标记对应的 ASCII 码为 0
18+
bitSet[s[left]] = 0
19+
left++
20+
}
21+
result = max(result, right-left)
22+
}
23+
return result
24+
}
25+
26+
// 解法二 滑动窗口
27+
func lengthOfLongestSubstring_(s string) int {
428
if len(s) == 0 {
529
return 0
630
}
731
var freq [256]int
8-
result := 0
9-
left, right := 0, -1
32+
result, left, right := 0, 0, -1
1033

1134
for left < len(s) {
1235
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {

leetcode/0003.Longest-Substring-Without-Repeating-Characters/3. Longest Substring Without Repeating Characters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_Problem3(t *testing.T) {
5151

5252
for _, q := range qs {
5353
_, p := q.ans3, q.para3
54-
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring(p.s))
54+
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring_(p.s))
5555
}
5656
fmt.Printf("\n\n\n")
5757
}

leetcode/0039.Combination-Sum/39. Combination Sum.go

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
2222
return
2323
}
2424
for i := index; i < len(nums); i++ {
25+
if nums[i] > target { // 这里可以剪枝优化
26+
break
27+
}
2528
c = append(c, nums[i])
2629
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
2730
c = c[:len(c)-1]

website/content/ChapterFour/0003.Longest-Substring-Without-Repeating-Characters.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,36 @@ Explanation: The answer is "wke", with the length of 3.
5555

5656
package leetcode
5757

58+
// 解法一 位图
5859
func lengthOfLongestSubstring(s string) int {
60+
if len(s) == 0 {
61+
return 0
62+
}
63+
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
64+
var bitSet [256]uint8
65+
result, left, right := 0, 0, 0
66+
for left < len(s) {
67+
if right < len(s) && bitSet[s[right]] == 0 {
68+
// 标记对应的 ASCII 码为 1
69+
bitSet[s[right]] = 1
70+
right++
71+
} else {
72+
// 标记对应的 ASCII 码为 0
73+
bitSet[s[left]] = 0
74+
left++
75+
}
76+
result = max(result, right-left)
77+
}
78+
return result
79+
}
80+
81+
// 解法二 滑动窗口
82+
func lengthOfLongestSubstring_(s string) int {
5983
if len(s) == 0 {
6084
return 0
6185
}
6286
var freq [256]int
63-
result := 0
64-
left, right := 0, -1
87+
result, left, right := 0, 0, -1
6588

6689
for left < len(s) {
6790
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
@@ -76,6 +99,14 @@ func lengthOfLongestSubstring(s string) int {
7699
return result
77100
}
78101

102+
func max(a int, b int) int {
103+
if a > b {
104+
return a
105+
}
106+
return b
107+
}
108+
109+
79110
```
80111

81112

website/content/ChapterFour/0039.Combination-Sum.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
7575
return
7676
}
7777
for i := index; i < len(nums); i++ {
78+
if nums[i] > target { // 这里可以剪枝优化
79+
break
80+
}
7881
c = append(c, nums[i])
7982
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
8083
c = c[:len(c)-1]
8184
}
8285
}
8386

84-
8587
```

0 commit comments

Comments
 (0)