Palindrome Number Problem LeetCode (Check if a number is a palindrome)

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

 Example 1:

Input: x = 121
Output: true

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101
Output: false

 NOTE:

  • -231 <= x <= 231 - 1

 Follow up: Could you solve it without converting the integer to a string?


This problem is popular in LeetCode, GeeksForGeeks, and many other forums - Ref 1, Ref 2 A collection of hundreds of interview questions and solutions are available in our blog at Interview Question Solutions
---------------------------------------------------------------------------
SOLUTION:
Source code: Java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Determine whether an integer is a palindrome. Do this without extra space.
*
* Some hints: Could negative integers be palindromes? (ie, -1)
*
* If you are thinking of converting the integer to string, note the restriction
* of using extra space.
*
* You could also try reversing an integer. However, if you have solved the
* problem "Reverse Integer", you know that the reversed integer might overflow.
* How would you handle such case?
*
* There is a more generic way of solving this problem. TODOS:
* Handle overflow
*/
/**
*
* @author rbaral
*
*/
public class PalindromeNumber {
public static void main(String[] args) {
boolean testEnabled = false;
if (testEnabled) {
performTest();
} else {
DateFormat format = new SimpleDateFormat("YYYY-M-d:H:m:s:S");
System.out.println(format.format(new Date()));
int num = 191;
System.out.println("output is:" + isPalindrome(num));
System.out.println(format.format(new Date()));
}
}
public static boolean isPalindrome(int x) {
if (x < 0)//negative numbers are not palindrome
{
return false;
}
if (x < 10)//one digit is always a palindrome
{
return true;
}
//find the reverse of the number and check if the reverse is equal to it
int rev = 0;
int temp = x;
while (temp > 0) {
rev = temp % 10 + rev * 10;
temp /= 10;
}
System.out.println("reverse is:" + rev + " number is:" + x);
if (rev == x) {
return true;
}
return false;
}
public static void performTest() {
Map<Integer, Boolean> testCases = new HashMap<Integer, Boolean>();
List<String> failedCases = new ArrayList<String>();
boolean result = false;
testCases.put(121, true);
testCases.put(122, false);
testCases.put(-2147483648, false);
testCases.put(11, true);
for (int testCase : testCases.keySet()) {
result = isPalindrome(testCase);
if (result != testCases.get(testCase)) {
failedCases.add("Failed for:{" + testCase + "} got:" + result + " expected:" + testCases.get(testCase));
}
}
System.out.println("TEST RESULT, Passed:" + (testCases.size() - failedCases.size()) + " failed:" + failedCases.size());
for (String failure : failedCases) {
System.err.println(failure);
}
}
}

No comments:

Post a Comment