Skip to content

Commit e9c9089

Browse files
committed
README
1 parent 7f085a5 commit e9c9089

File tree

3 files changed

+235
-3
lines changed

3 files changed

+235
-3
lines changed

Leetcode/0003.Longest-Substring-Without-Repeating-Characters/README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,104 @@ O(n)
4343

4444
## 來源
4545
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0003.Longest-Substring-Without-Repeating-Characters/
46-
* https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
46+
* https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
47+
48+
## 解答
49+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0003.Longest-Substring-Without-Repeating-Characters/Longest-Substring-Without-Repeating-Characters.go
50+
51+
```go
52+
package longestSubstringwithoutrepeatingcharacters
53+
54+
// LengthOfLongestSubstring 暴力解
55+
func LengthOfLongestSubstring(s string) int {
56+
slength := len(s)
57+
if slength == 0 || slength == 1 {
58+
return slength
59+
}
60+
61+
tmpLen := 1
62+
var maxLen = 1
63+
64+
for i := 1; i < slength; i++ {
65+
// 往前找前幾個視窗
66+
j := i - tmpLen
67+
68+
for ; j < i; j++ {
69+
if s[j] == s[i] { // 如果相同,那麼和S[J]到S[I-1]中間的肯定不相同,所以可以直接計算得到
70+
tmpLen = i - j
71+
break
72+
}
73+
}
74+
75+
if j == i { // 都不相同
76+
tmpLen++
77+
}
78+
79+
if tmpLen > maxLen {
80+
maxLen = tmpLen
81+
}
82+
}
83+
84+
return maxLen
85+
}
86+
87+
// LengthOfLongestSubstringMap 用map 紀錄是否重複.
88+
func LengthOfLongestSubstringMap(s string) int {
89+
slength := len(s)
90+
if slength == 0 || slength == 1 {
91+
return slength
92+
}
93+
94+
charMap := make(map[byte]bool)
95+
maxLen, left, right := 0, 0, 0
96+
for i := 0; i < slength; i++ {
97+
if ok := charMap[s[i]]; ok {
98+
// 有找到
99+
charMap[s[left]] = false
100+
left++
101+
} else {
102+
charMap[s[i]] = true
103+
right++
104+
}
105+
106+
if maxLen < right-left {
107+
maxLen = right - left
108+
}
109+
if left+maxLen >= slength || right >= len(s) {
110+
break
111+
}
112+
}
113+
return maxLen
114+
}
115+
116+
// LengthOfLongestSubstringBit 用map效能不好時可使用數組改善
117+
func LengthOfLongestSubstringBit(s string) int {
118+
slength := len(s)
119+
if slength == 0 || slength == 1 {
120+
return slength
121+
}
122+
123+
// ASCII 0~255
124+
charMap := [256]bool{}
125+
maxLen, left, right := 0, 0, 0
126+
for i := 0; i < slength; i++ {
127+
if ok := charMap[s[i]]; ok {
128+
// 有找到
129+
charMap[s[left]] = false
130+
left++
131+
} else {
132+
charMap[s[i]] = true
133+
right++
134+
}
135+
136+
if maxLen < right-left {
137+
maxLen = right - left
138+
}
139+
if left+maxLen >= slength || right >= len(s) {
140+
break
141+
}
142+
}
143+
return maxLen
144+
}
145+
146+
```

Leetcode/0015.3Sum/README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,91 @@ A solution set is:
3232

3333
## 來源
3434
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0015.3Sum/
35-
* https://leetcode-cn.com/problems/3sum/
35+
* https://leetcode-cn.com/problems/3sum/
36+
37+
38+
## 解答
39+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0015.3Sum/3Sum.go
40+
41+
```go
42+
package threesum
43+
44+
import (
45+
"sort"
46+
)
47+
48+
// ThreeSumBurst : 暴力解 : O(n^3)
49+
func ThreeSumBurst(nums []int) [][]int {
50+
result := [][]int{}
51+
sort.Ints(nums) // O(n log n)
52+
for i := 0; i < len(nums); i++ {
53+
// 需要跟上一次不同
54+
if i > 0 && nums[i] == nums[i-1] {
55+
continue
56+
}
57+
for j := i + 1; j < len(nums); j++ {
58+
// 需要跟上一次不同
59+
if j > i+1 && nums[j] == nums[j-1] {
60+
continue
61+
}
62+
for k := j + 1; k < len(nums); k++ {
63+
if nums[i]+nums[j]+nums[k] == 0 {
64+
result = append(result, []int{nums[i], nums[j], nums[k]})
65+
}
66+
}
67+
}
68+
}
69+
return result
70+
}
71+
72+
/*
73+
ThreeSumDoublePoint : 最佳解, 排序 + 雙指針法 (滑動視窗) O(n^2)
74+
1. 特判,對於數組長度 n,如果數組為 null 或者數組長度小於 3,返回 []。
75+
2. 對數組進行排序。
76+
3. 遍歷排序後數組:
77+
* 對於重複元素:跳過,避免出現重複解
78+
* 令左指針 L=i+1,右指針 R=n−1,當 L<R 時,執行循環:
79+
* 當nums[i]+nums[L]+nums[R]==0,執行循環,判斷左界和右界是否和下一位置重複,
80+
去除重複解。並同時將 L,R 移到下一位置,尋找新的解
81+
* 若和大於 0,說明 nums[R] 太大,R 左移
82+
* 若和小於 0,說明 nnums[L] 太小,L 右移
83+
*/
84+
func ThreeSumDoublePoint(nums []int) [][]int {
85+
result := [][]int{}
86+
if len(nums) < 3 {
87+
return result
88+
}
89+
sort.Ints(nums) // O(n log n)
90+
start, end, addNum := 0, 0, 0
91+
for i := 1; i < len(nums)-1; i++ {
92+
start, end = 0, len(nums)-1
93+
if i > 1 && nums[i] == nums[i-1] {
94+
// 去掉重複
95+
start = i - 1
96+
}
97+
for start < i && end > i {
98+
if start > 0 && nums[start] == nums[start-1] {
99+
// 去掉重複
100+
start++
101+
continue
102+
}
103+
if end < (len(nums)-1) && nums[end] == nums[end+1] {
104+
// 去掉重複
105+
end--
106+
continue
107+
}
108+
addNum = nums[start] + nums[end] + nums[i]
109+
if addNum == 0 {
110+
result = append(result, []int{nums[start], nums[i], nums[end]})
111+
start++
112+
end--
113+
} else if addNum > 0 {
114+
end--
115+
} else {
116+
start++
117+
}
118+
}
119+
}
120+
return result
121+
}
122+
```

Leetcode/0027.Remove-Element/README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,49 @@ for (int i = 0; i < len; i++) {
6262
## 來源
6363
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0027.Remove-Element/
6464
* https://leetcode-cn.com/problems/remove-element/
65-
* https://mp.weixin.qq.com/s/wj0T-Xs88_FHJFwayElQlA
65+
* https://mp.weixin.qq.com/s/wj0T-Xs88_FHJFwayElQlA
66+
67+
## 解答
68+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0027.Remove-Element/Remove-Element.go
69+
70+
```go
71+
package removeelement
72+
73+
/*
74+
雙指針法
75+
雙指針法(快慢指針法)在數組和鍊錶的操作中是非常常見的,很多考察數組和鍊錶操作的面試題,都使用雙指針法
76+
*/
77+
func RemoveElementDoublePoint(nums []int, val int) int {
78+
if len(nums) <= 0 {
79+
return 0
80+
}
81+
slowIndex := 0
82+
for fastIndex := 0; fastIndex < len(nums); fastIndex++ {
83+
if nums[fastIndex] != val {
84+
if fastIndex != slowIndex {
85+
nums[fastIndex], nums[slowIndex] = nums[slowIndex], nums[fastIndex]
86+
}
87+
slowIndex++
88+
}
89+
}
90+
return slowIndex
91+
}
92+
93+
/*
94+
RemoveElement :
95+
*/
96+
func RemoveElement(nums []int, val int) int {
97+
size := len(nums)
98+
i := 0
99+
for i < size {
100+
if nums[i] == val {
101+
nums = append(nums[:i], nums[i+1:]...)
102+
size--
103+
// fmt.Println(nums)
104+
} else {
105+
i++
106+
}
107+
}
108+
return len(nums)
109+
}
110+
```

0 commit comments

Comments
 (0)