美文网首页工作生活
[LeetCode 9] Palindrome Number (

[LeetCode 9] Palindrome Number (

作者: 灰睛眼蓝 | 来源:发表于2019-07-01 15:24 被阅读0次

    Don't convert the number into String

    public class Solution {
        public boolean isPalindrome(int x) {
            if (x < 0)  return false;
            
            //1. get divider
            int div = 1;
            int number = x;
            while (number / div >= 10) {
                div = div * 10;
            }
            
            System.out.println (div);
            // 2. find the palindrom
            while (div  != 0) {
                int first = number / div;
                int last = number % 10;
                
                if (first != last)
                    return false;
                number = (number - first * div) / 10;
                div = div / 100;
            }
             
            return true;
        }
        
    
    }
    

    相关文章

      网友评论

        本文标题:[LeetCode 9] Palindrome Number (

        本文链接:https://www.haomeiwen.com/subject/qfwgcctx.html