资讯专栏INFORMATION COLUMN

[LeetCode] 9. Palindrome Number

zhaochunqi / 897人阅读

Problem

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?

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72209.html

相关文章

  • javascript初探LeetCode9.Palindrome Number

    摘要:题目分析这是上的第题,难度为,判断整型数字是否为回文串,需要注意两点负数都不是回文小于的非负整数都是回文这一题与第题类似,也可以有两种思路数组法和模十法。所以代码可以如下改进能被整除的非整数和负数,返回 题目 Determine whether an integer is a palindrome. Do this without extra space. 分析 这是leetcode上...

    icyfire 评论0 收藏0
  • leetcode 9 Palindrome Number

    摘要:有一点需要注意的是,负数不算作回文数。而第题当时的方法是,对整数取除的余数,即是当前整数的最后一位。那么它翻转后一半的数字之后,应该和前半段的数字相等,我们将采用这种思路进行解题。 题目详情 Determine whether an integer is a palindrome. Do this without extra space.题目要求我们在不占用额外空间的前提下,判断一个整...

    darkbug 评论0 收藏0
  • Leetcode 9 Palindrome Number 回文数字判定

    摘要:难度本题要求判定一个整数是否为回文数字比如都是回文数字但是不是回文数字所有负数都不是回文数字本题还有一个关键要求不能使用额外空间我理解这里的额外空间是指堆空间在程序中不能去额外的什么变量更不用说提升空间复杂度直接上的解法解法 Determine whether an integer is a palindrome. Do this without extra space. Some ...

    ningwang 评论0 收藏0
  • [Leetcode] Palindrome Number 回文数

    摘要:反转比较法复杂度时间空间思路回文数有一个特性,就是它反转后值是一样的。代码逐位比较法复杂度时间空间思路反转比较有可能会溢出,但我们遍历每一位的时候其实并不用保存上一位的信息,只要和当前对应位相等就行了。首先,负数是否算回文。 Palindrome Number Determine whether an integer is a palindrome. Do this witho...

    _Suqin 评论0 收藏0
  • [LeetCode/LintCode] Largest Palindrome Product

    Problem Find the largest palindrome made from the product of two n-digit numbers. Since the result could be very large, you should return the largest palindrome mod 1337. Example Input: 2Output: 987Ex...

    Barry_Ng 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<