Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 24047be

Browse files
committed
fix leetcode 0008 with go
1 parent 887f232 commit 24047be

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 8. 字符串转换整数 (atoi)
2+
3+
## 链接
4+
https://leetcode-cn.com/problems/string-to-integer-atoi/
5+
6+
## 难度
7+
中等
8+
9+
## 描述
10+
请你来实现一个 atoi 函数,使其能将字符串转换成整数。
11+
12+
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
13+
14+
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
15+
16+
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
17+
18+
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
19+
20+
在任何情况下,若函数不能进行有效的转换时,请返回 0。
21+
22+
说明:
23+
24+
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2<sup>31</sup>, 2<sup>31</sup> − 1]。如果数值超过这个范围,qing返回 INT_MAX (2<sup>31</sup> − 1) 或 INT_MIN (−2<sup>31</sup>) 。
25+
26+
示例 1:
27+
```text
28+
输入: "42"
29+
输出: 42
30+
```
31+
示例 2:
32+
```text
33+
输入: " -42"
34+
输出: -42
35+
解释: 第一个非空白字符为 '-', 它是一个负号。
36+
我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。
37+
```
38+
示例 3:
39+
```text
40+
输入: "4193 with words"
41+
输出: 4193
42+
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。
43+
```
44+
示例 4:
45+
```text
46+
输入: "words and 987"
47+
输出: 0
48+
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
49+
因此无法执行有效的转换。
50+
```
51+
示例 5:
52+
```text1283472332"
53+
输出: -2147483648
54+
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。
55+
因此返回 INT_MIN (−231) 。
56+
```
57+
## 思路
58+
去两边空格,循环字符串,判断 ascii,转换成数字或者+-符号
59+
注意边界条件
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package _008_String_to_Integer_atoi
2+
3+
import (
4+
"math"
5+
"strings"
6+
)
7+
8+
func myAtoi(str string) int {
9+
str = strings.TrimSpace(str)
10+
if str == "" {
11+
return 0
12+
}
13+
if str[0] != 43 && str[0] != 45 && (str[0] < 48 || str[0] > 57) {
14+
return 0
15+
}
16+
17+
// +,43
18+
// -,45
19+
// 0-9,48-57
20+
symbol := 1
21+
r := 0
22+
if str[0] == 45 { // -
23+
symbol = -1
24+
} else if str[0] >= 48 { // 0-9
25+
r = int(str[0] - '0')
26+
}
27+
for _, v := range str[1:] {
28+
if v >= 48 && v <= 57 {
29+
r = r*10 + int(v-'0')
30+
if r >= math.MaxInt32+1 {
31+
r = math.MaxInt32 + 1
32+
break;
33+
}
34+
} else {
35+
break;
36+
}
37+
}
38+
r = symbol * r
39+
if r > math.MaxInt32 {
40+
r = math.MaxInt32
41+
} else if r < math.MinInt32 {
42+
r = math.MinInt32
43+
}
44+
return r
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package _008_String_to_Integer_atoi
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestOne(t *testing.T) {
9+
tests := []struct {
10+
s string
11+
r int
12+
}{
13+
{
14+
s: "42",
15+
r: 42,
16+
},
17+
{
18+
s: " -42",
19+
r: -42,
20+
},
21+
{
22+
s: "4193 with words",
23+
r: 4193,
24+
},
25+
{
26+
s: "words and 987",
27+
r: 0,
28+
},
29+
{
30+
s: "-91283472332",
31+
r: -2147483648,
32+
},
33+
{
34+
s: "9223372036854775808",
35+
r: 2147483647,
36+
},
37+
{
38+
s: "-91283472332",
39+
r: -2147483648,
40+
},
41+
}
42+
43+
for _, v := range tests {
44+
assert.Equal(t, myAtoi(v.s, ), v.r, "should be equal")
45+
}
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /