LeetCode_9. Palindrome Number

9. Palindrome Number

Easy

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?

package leetcode;

public class PalindromeNumber {
	@org.junit.Test
	public void test() {
		int number1 = 121;
		int number2 = -121;
		int number3 = 10;
		PalindromeNumber palindromeNumber = new PalindromeNumber();
		System.out.println(palindromeNumber.isPalindrome(number1));
		System.out.println(palindromeNumber.isPalindrome(number2));
		System.out.println(palindromeNumber.isPalindrome(number3));
	}

	public boolean isPalindrome(int x) {
		// Special cases:
		// As discussed above, when x < 0, x is not a palindrome.
		// Also if the last digit of the number is 0, in order to be a
		// palindrome,
		// the first digit of the number also needs to be 0.
		// Only 0 satisfy this property.
		if (x < 0 || (x % 10 == 0 && x != 0)) {
			return false;
		}

		int revertedNumber = 0;
		while (x > revertedNumber) {
			revertedNumber = revertedNumber * 10 + x % 10;
			x /= 10;
		}

		// When the length is an odd number, we can get rid of the middle digit
		// by revertedNumber/10
		// For example when the input is 12321, at the end of the while loop we
		// get x = 12, revertedNumber = 123,
		// since the middle digit doesn‘t matter in palidrome(it will always
		// equal to itself), we can simply get rid of it.
		return x == revertedNumber || x == revertedNumber / 10;
	}
}

原文地址:https://www.cnblogs.com/denggelin/p/11520815.html

时间: 2024-08-02 08:19:29

LeetCode_9. Palindrome Number的相关文章

leetcode_9题——Palindrome Number (数学问题)

Palindrome Number Total Accepted: 57795 Total Submissions: 194286My Submissions Question Solution Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Hide Tags Math Have you met this question in a real i

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 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

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number 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 spa

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

【LeetCode】Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restri

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

LeetCode 009 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题意] 题意判断一个整数是否是回文数 注意一下几点: 1. 不能用额外的空间 2. 负数不是回文数 [思路1] 依次比较首位值,需要注意处理类似1002001这样的数,当比较完首位值之后余下的数变成200, 2之前的两个0会自动清除. [代码] class Solution { public: int _size(int x){ int