Merge pull request #45 from halfrost/add_hugo

Optimized solution 3、39 & update README
This commit is contained in:
halfrost
2020-08-15 06:42:58 +08:00
committed by YDZ
6 changed files with 69 additions and 8 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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")
}

View File

@ -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]

View File

@ -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
}
```

View File

@ -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]
}
}
```