leetcode_13 Palindrome Number

161 篇文章 0 订阅

题目:

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?

判断是否为回文数字,从用例上看,负数不是回文数字,10不是回文数字,不能将数字转换为字符串进行判断

 

Approach 1: Revert half of the number

Intuition

The first idea that comes to mind is to convert the number into string, and check if the string is a palindrome, but this would require extra non-constant space for creating the string which is not allowed by the problem description.首先想到的是把数字转换为字符串判断是否为回文字符串,题目不允许(可以避免溢出问题)

Second idea would be reverting the number itself, and then compare the number with original number, if they are the same, then the number is a palindrome. However, if the reversed number is larger than int.MAX, we will hit integer overflow problem.第二种想法是逆序整个数字,与原数字比较,若相等则为回文数字,不过逆序数字可能溢出

Following the thoughts based on the second idea, to avoid the overflow issue of the reverted number, what if we only revert half of the int number? After all, the reverse of the last half of the palindrome should be the same as the first half of the number, if the number is a palindrome. 接下来的想法是对第二种方法的改进,避免数字逆序出现溢出,只逆序一半的数字,如果逆序的一半数字与剩下的一半相同,则为回文数字

For example, if the input is 1221, if we can revert the last part of the number "1221" from "21" to "12", and compare it with the first half of the number "12", since 12 is the same as 12, we know that the number is a palindrome.

Let's see how we could translate this idea into an algorithm.

Algorithm

First of all we should take care of some edge cases. All negative numbers are not palindrome, for example: -123 is not a palindrome since the '-' does not equal to '3'. So we can return false for all negative numbers.

Now let's think about how to revert the last half of the number. For number 1221, if we do 1221 % 10, we get the last digit 1, to get the second to the last digit, we need to remove the last digit from 1221, we could do so by dividing it by 10, 1221 / 10 = 122. Then we can get the last digit again by doing a modulus by 10, 122 % 10 = 2, and if we multiply the last digit by 10 and add the second last digit, 1 * 10 + 2 = 12, it gives us the reverted number we want. Continuing this process would give us the reverted number with more digits.

Now the question is, how do we know that we've reached the half of the number?

Since we divided the number by 10, and multiplied the reversed number by 10, when the original number is less than the reversed number, it means we've processed half of the number digits.

注意:1. 10,100,1000.......都不是回文数字,负数不是回文数字

           2. 怎样判断是否逆序了一半数字,开始 x  < rt  ,当不满足这个条件,说明已经逆序一半或超过一半数字

           3. 如果数字个数为奇数个,若 rt / 10 == x  , 则为回文数字;   若数字个数为偶数个,若 rt == x ,为回文数字

class Solution {
public:
    bool isPalindrome(int x) {
        if( x < 0 || ( x%10 == 0 && x!= 0))    return false;
        int rt = 0;
        while(x > rt)
        {
            rt = rt * 10 + x % 10;
            x = x/10;
        }
        
        if( x == rt || rt /10 == x)
            return true;
        return false;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值