/** * [7] Reverse Integer * * Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0. * Assume the environment does not allow you to store 64-bit integers (signed or unsigned). * * Example 1: * * Input: x = 123 * Output: 321 * * Example 2: * * Input: x = -123 * Output: -321 * * Example 3: * * Input: x = 120 * Output: 21 * * * Constraints: * * -2^31 <= x <= 2^31 - 1 * */ pub struct Solution {} // problem: https://leetcode.cn/problems/reverse-integer/ // discuss: https://leetcode.cn/problems/reverse-integer/discuss/?currentPage=1&orderBy=most_votes&query= // submission codes start here impl Solution { pub fn reverse(x: i32) -> i32 { let (num, negative) = if x < 0 { (-x, true) } else { (x, false) }; let str = num.to_string(); let mut str: String = str.chars().rev().collect(); if negative { str.insert(0, '-'); } str.parse::().unwrap_or_else(|_| 0) } } // submission codes end #[cfg(test)] mod tests { use super::*; #[test] fn test_7() { assert_eq!(321, Solution::reverse(123)); assert_eq!(-321, Solution::reverse(-123)); assert_eq!(21, Solution::reverse(120)); assert_eq!(0, Solution::reverse(0)); } }