Determine the highest and lowest position,then compare them,no extra space


  • 0
    S
    public class Solution {
        public boolean isPalindrome(int x) {
            if (x < 0)
    			return false;
    		int bits = 1;
    		while (x / bits >= 10) {
    			bits *= 10;// Determine the highest position
    		}
    		while (x > 0) {
    			int high = x / bits;
    			int low = x % 10;
    			if (high != low)
    				return false;
    			x -= high * bits;
    			x /= 10;
    			bits /= 100;// Because compare once,remove the first and last digits.
    		}
    		return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.