Java Solution (beats 82%)


  • 0
    Q

    Count the digits of the given integer first, then compare the highest and lowest digits. If they are equal, subtract them and start the next round with the remaining integer.

    public boolean isPalindrome(int x) {
            if (x < 0) {
    			return false;
    		}
    		int i = 0;
    		int temp = x;
    		while (temp / 10 > 0) {
    			temp = temp / 10;
    			i++;
    		}
    		
    		for (int j = 0; j < (i + 1) / 2; j++) {
    			int low = x % 10;
    			int dividant = (int) Math.pow(10, i - j * 2);
    			int high = x / dividant;
    			if (low != high) {
    				return false;
    			}
    			x = (x % dividant) / 10;
    		}
    		return true;
        }

Log in to reply
 

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