Skip to content

Commit 7b2aa19

Browse files
committed
update
1 parent a416e08 commit 7b2aa19

File tree

20 files changed

+607
-523
lines changed

20 files changed

+607
-523
lines changed

create/run.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ var (
4141
func Create(path string) {
4242

4343
if of, oerr := os.Stat(path); oerr == nil && of.IsDir() {
44-
log.Fatalf("%s is dir", path)
44+
log.Printf("%s is dir", path)
45+
return
4546
} else if os.IsExist(oerr) {
46-
log.Fatalf("%s is exist", path)
47+
log.Printf("%s is exist", path)
48+
return
4749
}
4850

4951
log.Printf("path is %s", path)
+93-93
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,93 @@
1-
package integer
2-
3-
import (
4-
"math"
5-
"strings"
6-
)
7-
8-
func myAtoi(str string) int {
9-
str = strings.TrimSpace(str)
10-
11-
ls := len(str)
12-
if ls == 0 {
13-
return 0
14-
}
15-
16-
myi, m, plus, less := 0, 1, false, false
17-
if str[0] == '-' {
18-
str = str[1:]
19-
less = true
20-
} else if str[0] == '+' {
21-
str = str[1:]
22-
plus = true
23-
}
24-
25-
if plus || less {
26-
ls--
27-
}
28-
29-
for k, v := range str {
30-
if v < 48 || v > 57 {
31-
str = str[:k]
32-
ls = k
33-
break
34-
}
35-
}
36-
for i := ls - 1; i >= 0; i-- {
37-
myi += int(str[i]-48) * m
38-
if isOver(myi) {
39-
if less {
40-
return math.MinInt32
41-
}
42-
return math.MaxInt32
43-
}
44-
m *= 10
45-
}
46-
47-
if less {
48-
myi = ^myi + 1
49-
}
50-
return myi
51-
}
52-
53-
func isOver(i int) bool {
54-
if i > math.MaxInt32 {
55-
return true
56-
} else if i < math.MinInt32 {
57-
return true
58-
}
59-
return false
60-
}
61-
62-
func atoi(s string) int {
63-
s = strings.TrimSpace(s)
64-
if s == "" || s == "0" {
65-
return 0
66-
}
67-
firsts := s[0]
68-
sign := 1
69-
start := 0
70-
res := 0
71-
if firsts == '+' {
72-
sign = 1
73-
start++
74-
} else if firsts == '-' {
75-
sign = -1
76-
start++
77-
}
78-
79-
for i := start; i < len(s); i++ {
80-
if s[i] < '0' || s[i] > '9' {
81-
return res * sign
82-
}
83-
p := int(s[i] - '0')
84-
res = res*10 + p
85-
if sign == 1 && res > math.MaxInt32 {
86-
return math.MaxInt32
87-
}
88-
if sign == -1 && res > math.MaxInt32 {
89-
return math.MinInt32
90-
}
91-
}
92-
return res * sign
93-
}
1+
package integer
2+
3+
import (
4+
"math"
5+
"strings"
6+
)
7+
8+
func myAtoi(str string) int {
9+
str = strings.TrimSpace(str)
10+
11+
ls := len(str)
12+
if ls == 0 {
13+
return 0
14+
}
15+
16+
myi, m, plus, less := 0, 1, false, false
17+
if str[0] == '-' {
18+
str = str[1:]
19+
less = true
20+
} else if str[0] == '+' {
21+
str = str[1:]
22+
plus = true
23+
}
24+
25+
if plus || less {
26+
ls--
27+
}
28+
29+
for k, v := range str {
30+
if v < 48 || v > 57 {
31+
str = str[:k]
32+
ls = k
33+
break
34+
}
35+
}
36+
for i := ls - 1; i >= 0; i-- {
37+
myi += int(str[i]-48) * m
38+
if isOver(myi) {
39+
if less {
40+
return math.MinInt32
41+
}
42+
return math.MaxInt32
43+
}
44+
m *= 10
45+
}
46+
47+
if less {
48+
myi = ^myi + 1
49+
}
50+
return myi
51+
}
52+
53+
func isOver(i int) bool {
54+
if i > math.MaxInt32 {
55+
return true
56+
} else if i < math.MinInt32 {
57+
return true
58+
}
59+
return false
60+
}
61+
62+
func atoi(s string) int {
63+
s = strings.TrimSpace(s)
64+
if s == "" || s == "0" {
65+
return 0
66+
}
67+
firsts := s[0]
68+
sign := 1
69+
start := 0
70+
res := 0
71+
if firsts == '+' {
72+
sign = 1
73+
start++
74+
} else if firsts == '-' {
75+
sign = -1
76+
start++
77+
}
78+
79+
for i := start; i < len(s); i++ {
80+
if s[i] < '0' || s[i] > '9' {
81+
return res * sign
82+
}
83+
p := int(s[i] - '0')
84+
res = res*10 + p
85+
if sign == 1 && res > math.MaxInt32 {
86+
return math.MaxInt32
87+
}
88+
if sign == -1 && res > math.MaxInt32 {
89+
return math.MinInt32
90+
}
91+
}
92+
return res * sign
93+
}
+19-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package integer
2-
3-
import "testing"
4-
5-
func TestMyAtoi(t *testing.T) {
6-
// s := "-123a..."
7-
// s := " 010"
8-
// s := "-2147483648"
9-
s := "2147483648"
10-
t.Log(myAtoi(s))
11-
}
12-
13-
func TestAtoi(t *testing.T) {
14-
s := "-123a..."
15-
// s := " 010"
16-
// s := "-2147483648"
17-
// s := "2147483648"
18-
t.Log(atoi(s))
19-
}
1+
package integer
2+
3+
import "testing"
4+
5+
func TestMyAtoi(t *testing.T) {
6+
// s := "-123a..."
7+
// s := " 010"
8+
// s := "-2147483648"
9+
s := "2147483648"
10+
t.Log(myAtoi(s))
11+
}
12+
13+
func TestAtoi(t *testing.T) {
14+
s := "-123a..."
15+
// s := " 010"
16+
// s := "-2147483648"
17+
// s := "2147483648"
18+
t.Log(atoi(s))
19+
}

0 commit comments

Comments
 (0)