mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
Merge pull request #45 from halfrost/add_hugo
Optimized solution 3、39 & update README
This commit is contained in:
@ -8,7 +8,7 @@
|
||||
</p>
|
||||
|
||||
<p align='center'>
|
||||
<a href="https://leetcode.com/halfrost/"><img src="https://img.shields.io/badge/@halfrost-9650-yellow.svg">
|
||||
<a href="https://leetcode.com/halfrost/"><img src="https://img.shields.io/badge/@halfrost-8751-yellow.svg">
|
||||
<img src="https://img.shields.io/badge/build-passing-brightgreen.svg">
|
||||
<img src="https://img.shields.io/badge/language-Golang-abcdef.svg">
|
||||
<a href="https://halfrost.com"><img src="https://img.shields.io/badge/Blog-Halfrost--Field-80d4f9.svg?style=flat"></a>
|
||||
@ -26,13 +26,15 @@
|
||||
</p>
|
||||
|
||||
<p align='center'>
|
||||
支持 Progressive Web Apps 的题解电子书《LeetCode Cookbook》在线阅读 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">地址</a>
|
||||
支持 Progressive Web Apps 的题解电子书《LeetCode Cookbook》 <a href="https://books.halfrost.com/leetcode/" rel="nofollow">Online Reading</a>
|
||||
</p>
|
||||
|
||||
## Data Structures
|
||||
|
||||
> 标识了 ✅ 的专题是完成所有题目了的,没有标识的是还没有做完所有题目的
|
||||
|
||||
<a href="https://books.halfrost.com/leetcode/"><img src="./website/static/logo.png" alt="logo" height="550" align="right" /></a>
|
||||
|
||||
* [Array](#array)
|
||||
* [String](#string)
|
||||
* [Two Pointers ✅](#two-pointers)
|
||||
|
@ -1,12 +1,35 @@
|
||||
package leetcode
|
||||
|
||||
// 解法一 位图
|
||||
func lengthOfLongestSubstring(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
|
||||
var bitSet [256]uint8
|
||||
result, left, right := 0, 0, 0
|
||||
for left < len(s) {
|
||||
if right < len(s) && bitSet[s[right]] == 0 {
|
||||
// 标记对应的 ASCII 码为 1
|
||||
bitSet[s[right]] = 1
|
||||
right++
|
||||
} else {
|
||||
// 标记对应的 ASCII 码为 0
|
||||
bitSet[s[left]] = 0
|
||||
left++
|
||||
}
|
||||
result = max(result, right-left)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 解法二 滑动窗口
|
||||
func lengthOfLongestSubstring_(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
var freq [256]int
|
||||
result := 0
|
||||
left, right := 0, -1
|
||||
result, left, right := 0, 0, -1
|
||||
|
||||
for left < len(s) {
|
||||
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
|
||||
|
@ -51,7 +51,7 @@ func Test_Problem3(t *testing.T) {
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans3, q.para3
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring(p.s))
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, lengthOfLongestSubstring_(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
|
@ -22,6 +22,9 @@ func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
|
||||
return
|
||||
}
|
||||
for i := index; i < len(nums); i++ {
|
||||
if nums[i] > target { // 这里可以剪枝优化
|
||||
break
|
||||
}
|
||||
c = append(c, nums[i])
|
||||
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
|
||||
c = c[:len(c)-1]
|
||||
|
@ -55,13 +55,36 @@ Explanation: The answer is "wke", with the length of 3.
|
||||
|
||||
package leetcode
|
||||
|
||||
// 解法一 位图
|
||||
func lengthOfLongestSubstring(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
// 扩展 ASCII 码的位图表示(BitSet),共有 256 位
|
||||
var bitSet [256]uint8
|
||||
result, left, right := 0, 0, 0
|
||||
for left < len(s) {
|
||||
if right < len(s) && bitSet[s[right]] == 0 {
|
||||
// 标记对应的 ASCII 码为 1
|
||||
bitSet[s[right]] = 1
|
||||
right++
|
||||
} else {
|
||||
// 标记对应的 ASCII 码为 0
|
||||
bitSet[s[left]] = 0
|
||||
left++
|
||||
}
|
||||
result = max(result, right-left)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 解法二 滑动窗口
|
||||
func lengthOfLongestSubstring_(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
var freq [256]int
|
||||
result := 0
|
||||
left, right := 0, -1
|
||||
result, left, right := 0, 0, -1
|
||||
|
||||
for left < len(s) {
|
||||
if right+1 < len(s) && freq[s[right+1]-'a'] == 0 {
|
||||
@ -76,6 +99,14 @@ func lengthOfLongestSubstring(s string) int {
|
||||
return result
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
@ -75,11 +75,13 @@ func findcombinationSum(nums []int, target, index int, c []int, res *[][]int) {
|
||||
return
|
||||
}
|
||||
for i := index; i < len(nums); i++ {
|
||||
if nums[i] > target { // 这里可以剪枝优化
|
||||
break
|
||||
}
|
||||
c = append(c, nums[i])
|
||||
findcombinationSum(nums, target-nums[i], i, c, res) // 注意这里迭代的时候 index 依旧不变,因为一个元素可以取多次
|
||||
c = c[:len(c)-1]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user