-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
88 lines (83 loc) · 2.87 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
fn main() {
println!("Hello, world!");
// let ret = Solution::my_atoi(String::from(" -0012a42"));
// print!("{}", ret);
println!("{:?}", (255 as u8).checked_mul(2).unwrap());
}
pub struct Solution {}
impl Solution {
pub fn my_atoi(input: String) -> i32 {
let vec = input.as_bytes();
let len = vec.len();
if len == 0 {
return 0;
}
let mut num_start = 0;
let mut sign = 1;
for i in num_start..len {
if vec[i] as char != ' ' {
num_start = i;
break;
}
}
use std::collections::HashSet;
let mut digits = HashSet::new();
for i in ('0' as u8)..('9' as u8 + 1) {
digits.insert(i as char);
}
if vec[num_start] as char == '-' {
sign = -1;
num_start += 1;
} else if vec[num_start] as char == '+' {
num_start += 1;
}
let mut sum = 0;
for i in num_start..len {
let c_char = vec[i] as char;
if ! digits.contains(&c_char) {
return sign * sum;
}
let d = vec[i] - '0' as u8;
if sum.checked_mul(10).is_none() {
if sign == 1 {
return i32::max_value();
} else {
return i32::min_value();
}
}
sum *= 10;
if sum.checked_add(d as i32).is_none() {
if sign == 1 {
return i32::max_value();
} else {
return i32::min_value();
}
}
sum += d as i32;
}
// now num_start points to the start of the number
println!("{}", sign);
sign * sum
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic() {
assert_eq!(Solution::my_atoi(String::from("42")), 42);
assert_eq!(Solution::my_atoi(String::from(" -42")), -42);
assert_eq!(Solution::my_atoi(String::from("4193 with words")), 4193);
assert_eq!(Solution::my_atoi(String::from("words and 987")), 0);
assert_eq!(Solution::my_atoi(String::from(" +4193")), 4193);
assert_eq!(Solution::my_atoi(String::from("-91283472332")), -2147483648);
assert_eq!(Solution::my_atoi(String::from(" -0012a42")), -12);
assert_eq!(Solution::my_atoi(String::from("2147483648")), 2147483647);
assert_eq!(Solution::my_atoi(String::from(" -42")), -42);
assert_eq!(Solution::my_atoi(String::from("4193 with words")), 4193);
assert_eq!(Solution::my_atoi(String::from("words and 987")), 0);
assert_eq!(Solution::my_atoi(String::from(" +4193")), 4193);
assert_eq!(Solution::my_atoi(String::from("-91283472332")), -2147483648);
assert_eq!(Solution::my_atoi(String::from(" -0012a42")), -12);
}
}