|
| 1 | +/// Given a signed 32-bit integer `x`, return `x` with its digits reversed. If |
| 2 | +/// reversing `x` causes the value to go outside the 32-bit integer range |
| 3 | +/// `[-2^31, 2^31 - 1]`, then return `0`. |
| 4 | +/// |
| 5 | +/// Assume the environment does not allow you to store 64-bit integers (signed |
| 6 | +/// or unsigned). |
| 7 | +struct Solution; |
| 8 | + |
| 9 | +impl Solution { |
| 10 | + |
| 11 | + pub fn reverse(x: i32) -> i32 { |
| 12 | + let is_negative = x < 0; |
| 13 | + let x = x.to_string(); |
| 14 | + let mut stack = Vec::new(); |
| 15 | + for c in x.chars() { |
| 16 | + match c { |
| 17 | + '1' => stack.push(1), |
| 18 | + '2' => stack.push(2), |
| 19 | + '3' => stack.push(3), |
| 20 | + '4' => stack.push(4), |
| 21 | + '5' => stack.push(5), |
| 22 | + '6' => stack.push(6), |
| 23 | + '7' => stack.push(7), |
| 24 | + '8' => stack.push(8), |
| 25 | + '9' => stack.push(9), |
| 26 | + '0' => stack.push(0), |
| 27 | + _ => { }, // ignore '-' also |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + let ten: i32 = 10; |
| 32 | + let mut result: i32 = 0; |
| 33 | + let mut place = stack.len() as u32; |
| 34 | + while !stack.is_empty() { |
| 35 | + place -= 1; |
| 36 | + let digit = stack.pop().unwrap(); |
| 37 | + if digit != 0 { |
| 38 | + let power = ten.checked_pow(place); |
| 39 | + let piece = power.and_then(|p| p.checked_mul(digit)); |
| 40 | + let check = piece.and_then(|p| result.checked_add(p)); |
| 41 | + if check.is_some() { |
| 42 | + result = check.unwrap(); |
| 43 | + } else { |
| 44 | + result = 0; |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + // Not possible to reverse and get i32::MAX or i32::MIN |
| 50 | + // so this one digit difference in i32::MAX and i32::MIN is irrelevant here. |
| 51 | + if is_negative { |
| 52 | + result *= -1; |
| 53 | + } |
| 54 | + |
| 55 | + result |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use super::Solution; |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn example_1() { |
| 66 | + let x = 123; |
| 67 | + let result = Solution::reverse(x); |
| 68 | + assert_eq!(result, 321); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn example_2() { |
| 73 | + let x = -123; |
| 74 | + let result = Solution::reverse(x); |
| 75 | + assert_eq!(result, -321); |
| 76 | + } |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn example_3() { |
| 80 | + let x = 120; |
| 81 | + let result = Solution::reverse(x); |
| 82 | + assert_eq!(result, 21); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn same_reversed() { |
| 87 | + let x = 1000000001; |
| 88 | + let result = Solution::reverse(x); |
| 89 | + assert_eq!(result, x); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn minimum() { |
| 94 | + let x = i32::MIN; |
| 95 | + let result = Solution::reverse(x); |
| 96 | + assert_eq!(result, 0); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn maximum() { |
| 101 | + let x = i32::MAX; |
| 102 | + let result = Solution::reverse(x); |
| 103 | + assert_eq!(result, 0); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments