Skip to content

Commit 911c0d4

Browse files
zsdostarasdf2014
zsdostar
authored andcommitted
enhance code quality (#44)
1 parent 77f6328 commit 911c0d4

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

Codes/zsdostar/2_add-two-numbers/solve.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import "fmt"
44

5+
// ListNode definition for singly-linked list.
56
type ListNode struct {
67
Val int
78
Next *ListNode

Codes/zsdostar/4_median-of-two-sorted-arrays/solve.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ func mergeTwoSortedArray(nums1, nums2 *[]int) []int {
3333
return nums
3434
}
3535
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
36+
var midRes float64
3637
totalLen := len(nums1) + len(nums2)
3738
nums := mergeTwoSortedArray(&nums1, &nums2)
3839
mid := totalLen / 2
3940

4041
if totalLen%2 == 1 {
41-
return float64(nums[mid])
42+
midRes = float64(nums[mid])
4243
} else {
43-
return (float64(nums[mid-1]) + float64(nums[mid])) / 2
44+
midRes = (float64(nums[mid-1]) + float64(nums[mid])) / 2
4445
}
46+
return midRes
4547
}
4648
func main() {
4749
fmt.Println(findMedianSortedArrays([]int{1}, []int{}))

Codes/zsdostar/6_zigzag-conversion/solve.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import "fmt"
44

55
// 题解: https://leetcode-cn.com/problems/zigzag-conversion/solution/bu-kuai-dan-te-wu-nao-de-yi-ge-jie-fa-_z_-by-zsdos/
6+
7+
// Min return the min value of two int.
68
func Min(x, y int) int {
79
if x < y {
810
return x

Codes/zsdostar/8_string-to-integer-atoi/solve.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ var ascii = map[int32]int{
1818
'9': 9,
1919
}
2020

21-
func myAtoi(str string) int {
22-
// 4 ms, 2.3 MB
23-
signed := ""
24-
temp, res := "", 0
21+
func cleanStr(str string) (string, string) {
22+
// 转化成为干净的数字字符串
23+
res, signed := "", ""
2524
// 死抠细节, 变态的数据。。
2625
for _, v := range str {
27-
if (v == ' ' || v == '-' || v == '+') && (len(temp) > 0 || signed != "") {
26+
if (v == ' ' || v == '-' || v == '+') && (len(res) > 0 || signed != "") {
2827
break
2928
}
3029
switch v {
@@ -40,15 +39,11 @@ func myAtoi(str string) int {
4039
if _, ok := ascii[v]; !ok {
4140
break
4241
}
43-
temp += string(v)
44-
}
45-
// temp已经成为干净的数字字符串,只需按位转为int即可
46-
for k, v := range temp {
47-
res += int(float64(ascii[v]) * math.Pow(10.0, float64(len(temp)-k-1)))
48-
}
49-
if signed == "-" {
50-
res = -res
42+
res += string(v)
5143
}
44+
return res, signed
45+
}
46+
func checkOverflow(res int, signed string) int {
5247
// 注意太大值溢出变为负数的问题
5348
max, min := math.MaxInt32, -(math.MaxInt32 + 1)
5449
if res > max {
@@ -64,9 +59,23 @@ func myAtoi(str string) int {
6459
}
6560
}
6661
}
67-
6862
return res
6963
}
64+
func myAtoi(str string) int {
65+
// 4 ms, 2.3 MB
66+
temp, signed := cleanStr(str)
67+
res := 0
68+
// 对于clean后的字符串, 只需按位转为int即可
69+
for k, v := range temp {
70+
res += int(float64(ascii[v]) * math.Pow(10.0, float64(len(temp)-k-1)))
71+
}
72+
73+
if signed == "-" {
74+
res = -res
75+
}
76+
77+
return checkOverflow(res, signed)
78+
}
7079

7180
func main() {
7281
fmt.Println(myAtoi("2147483648"))

0 commit comments

Comments
 (0)