Determine whether an integer is a palindrome number. Palindrome numbers refer to integers that read in positive order (from left to right) and reverse order (from right to left).
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: Read from left to right at - 121. Read from right to left, 121-. So it's not a palindrome number.
Example 3:
Input: 10
Output: false
Explanation: Read from right to left, 01. So it's not a palindrome number.
Advance:
Can you solve this problem without converting integers to strings?
package com.ljm.easy.onetofifty; /** * Created by linjiaming * * Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same * backward as forward. * * Example 1: * * Input: 121 Output: true Example 2: * * Input: -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: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a * palindrome. Follow up: * * Coud you solve it without converting the integer to a string? * * Source: LeetCode link: https://leetcode-cn.com/problems/palindrome-number * Copyright belongs to the seizure network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source. */ public class PalindromeNumber { public static void main(String[] args) { boolean palindrome = isPalindrome(1210121); System.out.println(palindrome); } /** * @param x * @return */ public static boolean isPalindrome(int x) { if (x < 0) { return false; } else if (x == 0) { return true; } else { if (x % 10 == 0) { return false; } else { int temp = x; int result = 0; while (temp != 0) { int mod = temp % 10; result = result * 10 + mod; temp = temp / 10; } if (result == x) { return true; } else { return false; } } } } }