当前位置: 代码迷 >> 综合 >> Leetcode-Palindrome Number(java)
  详细解决方案

Leetcode-Palindrome Number(java)

热度:30   发布时间:2023-12-03 11:48:18.0

1 Description(描述)

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.
解释:从左到右,他是-121。从右到左,他是121-。因此他不是回文数。

Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
解释:从右到左为01。因此他不是回文数。

注:小数,负数没有回文数。

Follow up:
Could you solve it without converting the integer to a string?
你能不将整型数转化为字符串解决问题么?

注:问题其实就转换成Reverse Integer问题,只不过就是看Reverse Integer是否等于原数。

class Solution {public boolean isPalindrome(int x) {if(x < 0) return false;int input = x;int rev = 0;while (x != 0) {rev = rev * 10 + x % 10;x /= 10;}return rev == input;}
}

注:内容均来源于leetcode。

  相关解决方案