数据结构和算法

palindrome-number

2019-12-04  本文已影响0人  奔跑的蛙牛

Runtime: 8 ms, faster than 46.98% of Rust online submissions for Palindrome Number.
Memory Usage: 2.3 MB, less than 100.00% of Rust online submissions for Palindrome Number.

    pub fn is_palindrome(x: i32) -> bool {
        let mut x = x;
        if x < 0 || (x % 10 == 0 && x != 0) {
            return false;
        }
        let mut reverse_number = 0;
        while x > reverse_number {
            reverse_number = reverse_number * 10 + x % 10;
            x = x / 10;
        }
        return x == reverse_number || x == reverse_number / 10;
    }
上一篇下一篇

猜你喜欢

热点阅读