|
| 1 | +/** |
| 2 | + * [8] String to Integer (atoi) |
| 3 | + * |
| 4 | + * Implement <code><span>atoi</span></code> which converts a string to an integer. |
| 5 | + * |
| 6 | + * The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. |
| 7 | + * |
| 8 | + * The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. |
| 9 | + * |
| 10 | + * If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. |
| 11 | + * |
| 12 | + * If no valid conversion could be performed, a zero value is returned. |
| 13 | + * |
| 14 | + * Note: |
| 15 | + * |
| 16 | + * <ul> |
| 17 | + * <li>Only the space character <code>' '</code> is considered as whitespace character.</li> |
| 18 | + * <li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [-2^31, 2^31 - 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 - 1) or INT_MIN (-2^31) is returned.</li> |
| 19 | + * </ul> |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * |
| 24 | + * Input: "42" |
| 25 | + * Output: 42 |
| 26 | + * |
| 27 | + * |
| 28 | + * Example 2: |
| 29 | + * |
| 30 | + * |
| 31 | + * Input: " -42" |
| 32 | + * Output: -42 |
| 33 | + * Explanation: The first non-whitespace character is '-', which is the minus sign. |
| 34 | + * Then take as many numerical digits as possible, which gets 42. |
| 35 | + * |
| 36 | + * |
| 37 | + * Example 3: |
| 38 | + * |
| 39 | + * |
| 40 | + * Input: "4193 with words" |
| 41 | + * Output: 4193 |
| 42 | + * Explanation: Conversion stops at digit '3' as the next character is not a numerical digit. |
| 43 | + * |
| 44 | + * |
| 45 | + * Example 4: |
| 46 | + * |
| 47 | + * |
| 48 | + * Input: "words and 987" |
| 49 | + * Output: 0 |
| 50 | + * Explanation: The first non-whitespace character is 'w', which is not a numerical |
| 51 | + * digit or a +/- sign. Therefore no valid conversion could be performed. |
| 52 | + * |
| 53 | + * Example 5: |
| 54 | + * |
| 55 | + * |
| 56 | + * Input: "-91283472332" |
| 57 | + * Output: -2147483648 |
| 58 | + * Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. |
| 59 | + * Thefore INT_MIN (-2^31) is returned. |
| 60 | + * |
| 61 | + */ |
| 62 | +pub struct Solution {} |
| 63 | + |
| 64 | +// submission codes start here |
| 65 | + |
| 66 | +impl Solution { |
| 67 | + pub fn my_atoi(input: String) -> i32 { |
| 68 | + let (i32_min, i32_max) = (-2_i64.pow(31), 2_i64.pow(31) - 1); |
| 69 | + let mut result: i64 = 0; |
| 70 | + let mut minus = false; |
| 71 | + // simple state machine |
| 72 | + let mut num_matched = false; |
| 73 | + for ch in input.chars().into_iter() { |
| 74 | + if !num_matched { |
| 75 | + match ch { |
| 76 | + ' ' => {}, |
| 77 | + '0'...'9' => { |
| 78 | + num_matched = true; |
| 79 | + result = result * 10 + ch.to_digit(10).unwrap() as i64; |
| 80 | + }, |
| 81 | + '-' => { num_matched = true; minus = true; } |
| 82 | + '+' => { num_matched = true; } |
| 83 | + _ => return 0 |
| 84 | + } |
| 85 | + } else { |
| 86 | + match ch { |
| 87 | + '0'...'9' => { |
| 88 | + result = result * 10 + ch.to_digit(10).unwrap() as i64; |
| 89 | + if result > i32_max { break } |
| 90 | + }, |
| 91 | + _ => break |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + result = if minus { -result } else { result }; |
| 96 | + if result > i32_max { return i32_max as i32; } |
| 97 | + if result < i32_min { return i32_min as i32; } |
| 98 | + return result as i32; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// submission codes end |
| 103 | + |
| 104 | +#[cfg(test)] |
| 105 | +mod tests { |
| 106 | + use super::*; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_8() { |
| 110 | + assert_eq!(Solution::my_atoi("aa".to_string()), 0); |
| 111 | + assert_eq!(Solution::my_atoi("-91283472332".to_string()), -2147483648); |
| 112 | + assert_eq!(Solution::my_atoi("words and 987".to_string()), 0); |
| 113 | + assert_eq!(Solution::my_atoi("4193 with words".to_string()), 4193); |
| 114 | + assert_eq!(Solution::my_atoi("42".to_string()), 42); |
| 115 | + assert_eq!(Solution::my_atoi("004193333".to_string()), 4193333); |
| 116 | + } |
| 117 | +} |
0 commit comments