ARTICLE AD BOX
Following is my code:
public boolean isPalindrome(int x) { if (x == 0) return true; if (x < 0) return false; int digitsCount = (int) Math.log10(x); return isPalindromeHelper(x, 0, digitsCount); } private boolean isPalindromeHelper(int x, int leftIndex, int rightIndex) { int leftDigit = getDigit(x, leftIndex), rightDigit = getDigit(x, rightIndex); if (leftIndex == rightIndex) { return true; } else if (leftDigit != rightDigit) { System.out.println("leftDigit="+leftDigit + ", rightDigit=" + rightDigit); return false; } else { return isPalindromeHelper(x, leftIndex + 1, rightIndex - 1); } } private int getDigit(int x, int idx) { int digitsCount = (int) Math.log10(x); int divisor = ((int) Math.pow(10, digitsCount - idx)); if (divisor == 0) return x % 10; System.out.println("divisor=" + divisor+",(x/divisor) %10=" + (x/divisor)%10); return (x / divisor) % 10; }I'm trying to retrieve specific digit at index, but for some reason when used on an integer like: 11, getting digit at index 0 returns 11.
Can anyone please point out the issue with this? If you can, also show process of debugging?
