All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0007_reverse_integer.Solution.rs Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #Top_Interview_Questions #Math #Udemy_Integers
// #2024_09_04_Time_0_ms_(100.00%)_Space_2.1_MB_(18.54%)

impl Solution {
    pub fn reverse(x: i32) -> i32 {
        let mut x = x;
        let mut rev: i64 = 0; // Use i64 to handle overflow

        while x != 0 {
            rev = rev * 10 + (x % 10) as i64;
            x /= 10;
        }

        if rev > i32::MAX as i64 || rev < i32::MIN as i64 {
            return 0;
        }

        rev as i32
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy