leecode---09---Number, take the remainder and divide it---determine whether a number is a palindrome

 
meaning of the title
Determine if a number is a palindrome
 
 
analyze
32132132100
              /100
delete the 0 part
             %100
leave the part of 0
 
 
code
class Solution {
    public boolean isPalindrome(int x) {
        if (x < 0) return false;
        
        //calculate to the highest bit of x
        int div = 1;
        while (div * 10 <= x) {
            div = div * 10;
        }
        
        // Each time the first and last bits are taken out for judgment
        //Reduce two digits after judgment
        while (x != 0) {
            int left = x /div;
            int right = x % 10;
            if (left != right) return false;
            // Take the remainder and keep the last bit, and divide it to keep the front bit
            x = (x%div) / 10;
            div /= 100;
        }
        return true;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685516&siteId=291194637