Skip to content

Commit 28d7bfe

Browse files
author
Zhang Xiaodong
committed
solve 8
1 parent 11f9877 commit 28d7bfe

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

src/problem/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
mod p0004_median_of_two_sorted_arrays;mod p0007_reverse_integer;
1+
mod p0004_median_of_two_sorted_arrays;

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ mod s0003_longest_substring_without_repeating_characters;
44
mod s0005_longest_palindromic_substring;
55
mod s0006_zigzag_conversion;
66
mod s0007_reverse_integer;
7+
mod s0008_string_to_integer_atoi;
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* [8] 字符串转换整数 (atoi)
3+
*
4+
* 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。
5+
* 函数 myAtoi(string s) 的算法如下:
6+
* <ol>
7+
* 读入字符串并丢弃无用的前导空格
8+
* 检查下一个字符(假设还未到字符末尾)为正还是负号,读取该字符(如果有)。 确定最终结果是负数还是正数。 如果两者都不存在,则假定结果为正。
9+
* 读入下一个字符,直到到达下一个非数字字符或到达输入的结尾。字符串的其余部分将被忽略。
10+
* 将前面步骤读入的这些数字转换为整数(即,"123" -> 123, "0032" -> 32)。如果没有读入数字,则整数为 0 。必要时更改符号(从步骤 2 开始)。
11+
* 如果整数数超过 32 位有符号整数范围 [−2^31, 2^31 − 1] ,需要截断这个整数,使其保持在这个范围内。具体来说,小于 −2^31 的整数应该被固定为 −2^31 ,大于 2^31 − 1 的整数应该被固定为 2^31 − 1 。
12+
* 返回整数作为最终结果。
13+
* </ol>
14+
* 注意:
15+
*
16+
* 本题中的空白字符只包括空格字符 ' ' 。
17+
* 除前导空格或数字后的其余字符串外,请勿忽略 任何其他字符。
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入:s = "42"
23+
* 输出:42
24+
* 解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。
25+
* 第 1 步:"42"(当前没有读入字符,因为没有前导空格)
26+
* ^
27+
* 第 2 步:"42"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
28+
* ^
29+
* 第 3 步:"<u>42</u>"(读入 "42")
30+
* ^
31+
* 解析得到整数 42 。
32+
* 由于 "42" 在范围 [-2^31, 2^31 - 1] 内,最终结果为 42 。
33+
* 示例 2:
34+
*
35+
* 输入:s = " -42"
36+
* 输出:-42
37+
* 解释:
38+
* 第 1 步:"<u> </u>-42"(读入前导空格,但忽视掉)
39+
* ^
40+
* 第 2 步:" <u>-</u>42"(读入 '-' 字符,所以结果应该是负数)
41+
* ^
42+
* 第 3 步:" <u>-42</u>"(读入 "42")
43+
* ^
44+
* 解析得到整数 -42 。
45+
* 由于 "-42" 在范围 [-2^31, 2^31 - 1] 内,最终结果为 -42 。
46+
*
47+
* 示例 3:
48+
*
49+
* 输入:s = "4193 with words"
50+
* 输出:4193
51+
* 解释:
52+
* 第 1 步:"4193 with words"(当前没有读入字符,因为没有前导空格)
53+
* ^
54+
* 第 2 步:"4193 with words"(当前没有读入字符,因为这里不存在 '-' 或者 '+')
55+
* ^
56+
* 第 3 步:"<u>4193</u> with words"(读入 "4193";由于下一个字符不是一个数字,所以读入停止)
57+
* ^
58+
* 解析得到整数 4193 。
59+
* 由于 "4193" 在范围 [-2^31, 2^31 - 1] 内,最终结果为 4193 。
60+
*
61+
*
62+
* 提示:
63+
*
64+
* 0 <= s.length <= 200
65+
* s 由英文字母(大写和小写)、数字(0-9)、' '、'+'、'-' 和 '.' 组成
66+
*
67+
*/
68+
pub struct Solution {}
69+
70+
// problem: https://leetcode.cn/problems/string-to-integer-atoi/
71+
// discuss: https://leetcode.cn/problems/string-to-integer-atoi/discuss/?currentPage=1&orderBy=most_votes&query=
72+
73+
// submission codes start here
74+
75+
impl Solution {
76+
pub fn my_atoi(s: String) -> i32 {
77+
let mut start = false;
78+
let mut is_first_none_zero = true;
79+
let mut sig = 1;
80+
let mut res = 0i32;
81+
for c in s.chars() {
82+
if !start {
83+
if c == ' ' {
84+
continue;
85+
}
86+
if (c == '-' || c == '+' || c.is_digit(10)) {
87+
if c == '-' {
88+
sig = -1;
89+
} else if c.is_digit(10) {
90+
res = c.to_digit(10).unwrap() as i32;
91+
}
92+
start = true;
93+
continue;
94+
}
95+
} else {
96+
if !c.is_digit(10) {
97+
break;
98+
}
99+
if res.checked_mul(10).is_none() {
100+
return if sig > 0 { std::i32::MAX } else { std::i32::MIN };
101+
}
102+
res = res * 10 + c.to_digit(10).unwrap() as i32;
103+
if is_first_none_zero && res != 0 {
104+
res = res * sig;
105+
is_first_none_zero = false;
106+
}
107+
}
108+
}
109+
res
110+
}
111+
}
112+
113+
// submission codes end
114+
115+
#[cfg(test)]
116+
mod tests {
117+
use crate::solution;
118+
119+
use super::*;
120+
121+
#[test]
122+
fn test_8() {
123+
assert_eq!(Solution::my_atoi("10".to_string()), 10);
124+
assert_eq!(Solution::my_atoi(" ".to_string()), 0);
125+
assert_eq!(Solution::my_atoi(" -10".to_string()), -10);
126+
assert_eq!(Solution::my_atoi(" 10".to_string()), 10);
127+
assert_eq!(Solution::my_atoi(" 10 ".to_string()), 10);
128+
}
129+
}

0 commit comments

Comments
 (0)