Skip to content

Commit 2897fbf

Browse files
committed
update
1 parent c05abee commit 2897fbf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1419
-757
lines changed

000xxx/010.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
func isMatch(s string, p string) bool {
2-
ls, lp := len(s), len(p)
3-
if 0 == lp {
4-
return 0 == ls
2+
m, n := len(s), len(p)
3+
t := make([][]bool, m+1)
4+
t[0] = make([]bool, n+1)
5+
t[0][0] = true
6+
for j := 2; j <= n; j++ {
7+
if p[j-1] == '*' {
8+
t[0][j] = t[0][j-2]
9+
}
510
}
6-
if lp > 1 && p[1] == '*' {
7-
return isMatch(s, p[2:]) || (ls > 0 && (s[0] == p[0] || p[0] == '.') && isMatch(s[1:], p))
8-
} else {
9-
return ls > 0 && (s[0] == p[0] || p[0] == '.') && isMatch(s[1:], p[1:])
11+
for i := 1; i <= m; i++ {
12+
t[i] = make([]bool, n+1)
13+
if 0 != n {
14+
t[i][1] = t[i-1][0] && (s[i-1] == p[0] || p[0] == '.')
15+
}
16+
for j := 2; j <= n; j++ {
17+
if p[j-1] == '*' {
18+
t[i][j] = t[i][j-2] || ((s[i-1] == p[j-2] || p[j-2] == '.') && t[i-1][j])
19+
} else {
20+
t[i][j] = t[i-1][j-1] && (s[i-1] == p[j-1] || p[j-1] == '.')
21+
}
22+
}
1023
}
24+
return t[m][n]
1125
}

000xxx/011.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
func min(a, b int) int {
2+
if a <= b {
3+
return a
4+
}
5+
return b
6+
}
7+
18
func maxArea(height []int) int {
2-
out := 0
9+
o := 0
310
i, j := 0, len(height)-1
411
for i != j {
5-
h := height[i]
6-
if h > height[j] {
7-
h = height[j]
8-
}
9-
s := (j - i) * h
10-
if s > out {
11-
out = s
12+
hi, hj := height[i], height[j]
13+
s := (j - i) * min(hi, hj)
14+
if s > o {
15+
o = s
1216
}
1317

14-
if height[i] > height[j] {
18+
if hi > hj {
1519
j--
1620
} else {
1721
i++
1822
}
1923
}
20-
return out
24+
return o
2125
}

000xxx/012.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
var t = [4]string{"", "M", "MM", "MMM"}
2+
var h = [10]string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
3+
var d = [10]string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
4+
var u = [10]string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
5+
16
func intToRoman(num int) string {
2-
t := []string{"", "M", "MM", "MMM"}
3-
h := []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
4-
d := []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
5-
u := []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
67
return t[num/1000] + h[(num%1000)/100] + d[(num%100)/10] + u[num%10]
78
}

000xxx/013.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
var m = map[byte]int{'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
2+
13
func romanToInt(s string) int {
2-
m := map[byte]int{'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
3-
n := 0
4-
for i, c := range []byte(s) {
5-
if i == len(s)-1 || m[c] >= m[s[i+1]] {
6-
n += m[c]
4+
o, l := 0, len(s)
5+
for i := 0; i < l; i++ {
6+
v := m[s[i]]
7+
if i == l-1 || v >= m[s[i+1]] {
8+
o += v
79
} else {
8-
n -= m[c]
10+
o -= v
911
}
1012
}
11-
return n
13+
return o
1214
}

000xxx/016.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import "sort"
1+
import (
2+
"math"
3+
"sort"
4+
)
25

36
func threeSumClosest(nums []int, target int) int {
7+
o, l, d := 0, len(nums), math.MaxInt64
48
sort.Ints(nums)
5-
var diff *int
6-
out := 0
7-
8-
for i := 0; i < len(nums)-2; i++ {
9-
j, k := i+1, len(nums)-1
9+
for i := 0; i < l-2; i++ {
10+
j, k := i+1, l-1
1011
for j < k {
11-
sum := nums[i] + nums[j] + nums[k]
12-
d := sum - target
13-
if d < 0 {
14-
d = -d
12+
s := nums[i] + nums[j] + nums[k]
13+
d1 := s - target
14+
if d1 < 0 {
15+
d1 = -d1
1516
}
16-
if (nil != diff && d < *diff) || nil == diff {
17-
diff = &d
18-
out = sum
17+
if d1 < d {
18+
d, o = d1, s
1919
}
20-
if sum > target {
20+
if s > target {
2121
k--
2222
} else {
2323
j++
2424
}
2525
}
2626
}
27-
return out
27+
return o
2828
}

000xxx/017.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1-
var m = map[byte][]string{
2-
'2': {"a", "b", "c"},
3-
'3': {"d", "e", "f"},
4-
'4': {"g", "h", "i"},
5-
'5': {"j", "k", "l"},
6-
'6': {"m", "n", "o"},
7-
'7': {"p", "q", "r", "s"},
8-
'8': {"t", "u", "v"},
9-
'9': {"w", "x", "y", "z"},
1+
var m = [9][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"},
2+
{"m", "n", "o"}, {"p", "q", "r", "s"}, {"t", "u", "v"}, {"w", "x", "y", "z"},
103
}
114

125
func letterCombinations(digits string) []string {
13-
ss := []string{}
6+
o := []string{}
147
if 0 == len(digits) {
15-
return ss
8+
return o
169
}
17-
bs := m[digits[0]]
10+
bs := m[digits[0]-'2']
1811
if 1 == len(digits) {
1912
for _, b := range bs {
20-
ss = append(ss, b)
13+
o = append(o, b)
2114
}
22-
return ss
15+
return o
2316
}
2417
subs := letterCombinations(digits[1:])
2518
for _, b := range bs {
2619
for _, s := range subs {
27-
ss = append(ss, b+s)
20+
o = append(o, b+s)
2821
}
2922
}
30-
return ss
23+
return o
3124
}

000xxx/018.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
11
import "sort"
22

33
func fourSum(nums []int, target int) [][]int {
4+
o, l := [][]int{}, len(nums)
45
sort.Ints(nums)
5-
out := [][]int{}
6-
for i := 0; i < len(nums)-3; i++ {
7-
for j := i + 1; j < len(nums)-2; j++ {
8-
if j > i+1 && nums[j] == nums[j-1] {
6+
for i := 0; i < l-3; i++ {
7+
ni := nums[i]
8+
if i != 0 && ni == nums[i-1] {
9+
continue
10+
}
11+
if ni+nums[i+1]+nums[i+2]+nums[i+3] > target {
12+
break
13+
}
14+
if ni+nums[l-1]+nums[l-2]+nums[l-2] < target {
15+
continue
16+
}
17+
for j := i + 1; j < l-2; j++ {
18+
nj := nums[j]
19+
if j != i+1 && nj == nums[j-1] {
920
continue
1021
}
11-
k, l := j+1, len(nums)-1
12-
for k < l {
13-
s := nums[i] + nums[j] + nums[k] + nums[l]
14-
if s == target {
15-
ok := true
16-
for _, o := range out {
17-
if o[0] == nums[i] && o[1] == nums[j] && o[2] == nums[k] && o[3] == nums[l] {
18-
ok = false
19-
break
20-
}
21-
}
22-
if ok {
23-
out = append(out, []int{nums[i], nums[j], nums[k], nums[l]})
22+
if ni+nj+nums[j+1]+nums[j+2] > target {
23+
break
24+
}
25+
if ni+nj+nums[l-1]+nums[l-2] < target {
26+
continue
27+
}
28+
b, e := j+1, l-1
29+
for b < e {
30+
t := ni + nj + nums[b] + nums[e]
31+
if t == target {
32+
o, b = append(o, []int{ni, nj, nums[b], nums[e]}), b+1
33+
for b < e && nums[b] == nums[b-1] {
34+
b++
2435
}
25-
k++
26-
l--
27-
} else if s < target {
28-
k++
36+
} else if t > target {
37+
e--
2938
} else {
30-
l--
39+
b++
3140
}
3241
}
3342
}
3443
}
35-
return out
44+
return o
3645
}

000xxx/019.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
44
nodes = append(nodes, head)
55
head = head.Next
66
}
7-
if len(nodes) <= 1 {
7+
l := len(nodes)
8+
if l <= 1 {
89
return nil
910
}
1011
if 1 == n {
11-
nodes[len(nodes)-2].Next = nil
12+
nodes[l-2].Next = nil
1213
return nodes[0]
13-
} else if n < len(nodes) {
14-
nodes[len(nodes)-n-1].Next = nodes[len(nodes)-n+1]
14+
} else if n < l {
15+
nodes[l-n-1].Next = nodes[l-n+1]
1516
return nodes[0]
1617
}
1718
return nodes[1]

000xxx/022.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
func generateParenthesis(n int) []string {
2-
strs := []string{}
3-
var gen func(int, int, int, string)
4-
gen = func(a, b, n int, s string) {
2+
o, bs := []string{}, []byte{}
3+
var cal func(int, int)
4+
cal = func(a, b int) {
55
if a == n && b == n {
6-
strs = append(strs, s)
6+
o = append(o, string(bs))
77
return
88
}
99
if b > a || b > n || a > n {
1010
return
1111
}
12-
gen(a+1, b, n, s+"(")
13-
gen(a, b+1, n, s+")")
12+
bs = append(bs, '(')
13+
cal(a+1, b)
14+
bs[len(bs)-1] = ')'
15+
cal(a, b+1)
16+
bs = bs[:len(bs)-1]
1417
}
15-
gen(0, 0, n, "")
16-
return strs
18+
cal(0, 0)
19+
return o
1720
}

000xxx/023.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
2-
h := &ListNode{}
3-
l := h
4-
for nil != l1 && nil != l2 {
5-
if l1.Val <= l2.Val {
6-
l.Next = l1
7-
l1 = l1.Next
1+
func merge2Lists(l1, l2 *ListNode) *ListNode {
2+
n := &ListNode{}
3+
h := n
4+
for l1 != nil && l2 != nil {
5+
if l1.Val < l2.Val {
6+
l1, n.Next = l1.Next, l1
87
} else {
9-
l.Next = l2
10-
l2 = l2.Next
8+
n.Next = l2
9+
l1, l2 = n.Next.Next, l1
1110
}
12-
l = l.Next
11+
n = n.Next
1312
}
1413
if l1 != nil {
15-
l.Next = l1
16-
} else {
17-
l.Next = l2
14+
n.Next = l1
15+
}
16+
if l2 != nil {
17+
n.Next = l2
1818
}
1919
return h.Next
2020
}
2121

2222
func mergeKLists(lists []*ListNode) *ListNode {
23-
if 0 == len(lists) {
23+
l := len(lists)
24+
if l == 0 {
2425
return nil
25-
} else if 1 == len(lists) {
26+
} else if l == 1 {
2627
return lists[0]
2728
}
28-
h := lists[0]
29-
for _, l := range lists[1:] {
30-
h = mergeTwoLists(h, l)
31-
}
32-
return h
29+
m := l >> 1
30+
return merge2Lists(mergeKLists(lists[:m]), mergeKLists(lists[m:]))
3331
}

0 commit comments

Comments
 (0)