Skip to content

Commit 66641d4

Browse files
committed
4,5,10,11,15,17
1 parent 6be3bff commit 66641d4

File tree

9 files changed

+478
-125
lines changed

9 files changed

+478
-125
lines changed

alg/2021/leetcode/10-1.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println(isMatch("aab", "c*a*b"))
9+
}
10+
11+
func isMatch(s string, p string) bool {
12+
arr1 := []byte(s)
13+
arr2 := []byte(p)
14+
15+
dfs := make([][]int, len(arr1)+1)
16+
17+
for i := 0; i < len(arr1)+1; i++ {
18+
tmp := make([]int, len(arr2)+1)
19+
dfs[i] = tmp
20+
}
21+
22+
return compare(arr1, arr2, dfs, 0, 0)
23+
}
24+
25+
func compare(s []byte, p []byte, dfs [][]int, i int, j int) bool {
26+
if dfs[i][j] != 0 {
27+
return dfs[i][j] == 1
28+
}
29+
len1 := len(s)
30+
len2 := len(p)
31+
flag := false
32+
if j == len2 {
33+
if i == len1 {
34+
flag = true
35+
}
36+
}
37+
38+
if j == len2-1 {
39+
if i < len1 {
40+
if s[i] == p[j] || p[j] == byte('.') {
41+
flag = compare(s, p, dfs, i+1, j+1)
42+
}
43+
}
44+
}
45+
46+
if j < len2-1 {
47+
if i < len1 && (s[i] == p[j] || p[j] == byte('.')) {
48+
if p[j+1] == byte('*') {
49+
flag = compare(s, p, dfs, i, j+2) || compare(s, p, dfs, i+1, j) || compare(s, p, dfs, i+1, j+2)
50+
} else {
51+
flag = compare(s, p, dfs, i+1, j+1)
52+
}
53+
} else {
54+
if p[j+1] == byte('*') {
55+
flag = compare(s, p, dfs, i, j+2)
56+
}
57+
}
58+
}
59+
60+
if flag == true {
61+
dfs[i][j] = 1
62+
} else {
63+
dfs[i][j] = -1
64+
65+
}
66+
return flag
67+
}

alg/2021/leetcode/10.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
fmt.Println(isMatch("fs", ".*.."))
9+
}
10+
11+
func isMatch(s string, p string) bool {
12+
arr1 := []byte(s)
13+
arr2 := []byte(p)
14+
15+
return compare(arr1, arr2)
16+
}
17+
18+
func compare(s []byte , p []byte) bool {
19+
len1 := len(s)
20+
len2 := len(p)
21+
22+
if len2 == 0 {
23+
return len1 == 0
24+
}
25+
26+
if len1 >= 1 {
27+
matched := (s[0] == p[0] || p[0] == byte('.'))
28+
29+
if matched {
30+
if len2 >= 2 && p[1] == byte('*') {
31+
return compare(s, p[2:]) || compare(s[1:], p)
32+
} else {
33+
return compare(s[1:], p[1:])
34+
}
35+
} else {
36+
if len2 >= 2 && p[1] == byte('*') {
37+
return compare(s, p[2:])
38+
} else {
39+
return false
40+
}
41+
}
42+
43+
} else if len2 >= 2{
44+
if p[1] == byte('*') {
45+
return compare(s, p[2:])
46+
}
47+
}
48+
49+
return false
50+
51+
}

alg/2021/leetcode/11-1.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
arr := []int{2,3,3,4,8,9,9,1}
9+
fmt.Println(maxArea(arr))
10+
}
11+
12+
func maxArea(height []int) int {
13+
max := 0
14+
rst := 0
15+
a := 0
16+
j := len(height) - 1
17+
for i:=0;;{
18+
a = height[i]
19+
if a > height[j] {
20+
a = height[j]
21+
}
22+
23+
rst = a*(j-i)
24+
if max < rst {
25+
max = rst
26+
}
27+
if height[i] < height[j] {
28+
i++
29+
} else {
30+
j--
31+
}
32+
if i==j {
33+
break
34+
}
35+
}
36+
37+
return max
38+
}

alg/2021/leetcode/11.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
arr := []int{2,3,3,4,8,9,9,1}
9+
fmt.Println(maxArea(arr))
10+
}
11+
12+
func maxArea(height []int) int {
13+
max := 0
14+
a := 0
15+
for i:=0;i<len(height);i++{
16+
for j:=i+1;j<len(height);j++ {
17+
a = height[i]
18+
if height[i] > height[j] {
19+
a = height[j]
20+
}
21+
rst := a * (j-i)
22+
if max < rst {
23+
max = rst
24+
}
25+
}
26+
}
27+
28+
return max
29+
}

alg/2021/leetcode/15.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
arr := []int{-4, -2, 0, 2, 3, 221, 23, 45, 63, 38, 2, 32, 5, 6}
9+
10+
fmt.Println(threeSum(arr))
11+
}
12+
13+
/**
14+
* 排序。双指针移动。
15+
*/
16+
func threeSum(nums []int) [][]int {
17+
if len(nums) < 3 {
18+
return make([][]int, 0)
19+
}
20+
rst := make([][]int, 0)
21+
22+
fastSort(nums, 0, len(nums)-1)
23+
24+
for i := 0; i < len(nums); i++ {
25+
if nums[i] > 0 {
26+
break
27+
}
28+
if i >= 1 && nums[i] == nums[i-1] {
29+
continue
30+
}
31+
left := i + 1
32+
right := len(nums) - 1
33+
lastLeft := left
34+
lastRight := right
35+
for left < right {
36+
if left != lastLeft && nums[left] == nums[lastLeft] {
37+
left++
38+
continue
39+
}
40+
if right != lastRight && nums[right] == nums[lastRight] {
41+
right--
42+
continue
43+
}
44+
45+
// 满足条件时,left和right同时滑动
46+
if nums[i]+nums[left]+nums[right] == 0 {
47+
add := make([]int, 0)
48+
add = append(add, nums[i], nums[left], nums[right])
49+
rst = append(rst, add)
50+
lastLeft = left
51+
lastRight = right
52+
left++
53+
right--
54+
} else if nums[i]+nums[left]+nums[right] > 0 {
55+
// 右滑
56+
lastRight = right
57+
right--
58+
59+
} else {
60+
// 左滑
61+
lastLeft = left
62+
left++
63+
}
64+
}
65+
}
66+
return rst
67+
}
68+
69+
func fastSort(arr []int, left int, right int) {
70+
if left >= right {
71+
return
72+
}
73+
l1 := left
74+
r1 := right
75+
middle := arr[left]
76+
77+
for left != right {
78+
79+
for right > left && arr[right] >= middle {
80+
right--
81+
}
82+
arr[left] = arr[right]
83+
for left < right && arr[left] <= middle {
84+
left++
85+
}
86+
arr[right] = arr[left]
87+
}
88+
arr[left] = middle
89+
fastSort(arr, l1, left-1)
90+
fastSort(arr, left+1, r1)
91+
92+
}

alg/2021/leetcode/17.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var rst []string
8+
9+
func main() {
10+
str := "623"
11+
// rst = append(rst, "abc")
12+
// fmt.Println(rst[0])
13+
fmt.Println(letterCombinations(str))
14+
}
15+
16+
17+
func letterCombinations(digits string) []string {
18+
if digits == "" {
19+
return make([]string, 0)
20+
}
21+
rst = make([]string, 0)
22+
mapd := map[string][]string{"2":{"a","b","c"}, "3":{"d","e","f"},"4":{"g","h","i"},"5":{"j","k","l"}, "6":{"m","n","o"}, "7":{"p","q","r","s"}, "8":{"t","u","v"},"9":{"w","x","y","z"}}
23+
// fmt.Println(mapd)
24+
25+
slice := make([][]string, 0)
26+
for i:=0;i<len(digits);i++{
27+
a := mapd[string(digits[i])]
28+
slice = append(slice, a)
29+
}
30+
// fmt.Println(slice)
31+
// fmt.Println(digits[2:3])
32+
// fmt.Println(string(digits[2]))
33+
// fmt.Println([]rune(digits))
34+
backTrace(slice, 0, "")
35+
return rst
36+
}
37+
38+
/*
39+
* go语言字符串操作: https://www.jianshu.com/p/61dc40f5090d
40+
* 回溯法,回溯所有组合
41+
*/
42+
func backTrace(slice [][]string, layer int, base string) {
43+
44+
if layer == len(slice) - 1 {
45+
for i:=0;i<len(slice[layer]);i++{
46+
tmp := base + string(slice[layer][i])
47+
rst = append(rst, tmp)
48+
// fmt.Println(rst)
49+
}
50+
} else {
51+
for i:=0;i<len(slice[layer]);i++ {
52+
backTrace(slice, layer+1, base+string(slice[layer][i]))
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)