• 为了保证你在浏览本网站时有着更好的体验,建议使用类似Chrome、Firefox之类的浏览器~~
    • 如果你喜欢本站的内容何不Ctrl+D收藏一下呢,与大家一起分享各种编程知识~
    • 本网站研究机器学习、计算机视觉、模式识别~当然不局限于此,生命在于折腾,何不年轻时多折腾一下

每日leetcode-Palindrome Number

leetcode admin 4年前 (2016-04-13) 1696次浏览 0个评论 扫描二维码

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 space.

You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.


 

这是最快的 OJ 题,一分钟搞定。。。。。

class Solution {
public:
    bool isPalindrome(int x) {
        int tmp=x;
        int result=0;
        while(x>0)
        {
            result=result*10+x%10;
            x=x/10;
        }
        return result==tmp;

    }
};

Deeplearn, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明每日 leetcode-Palindrome Number
喜欢 (0)
admin
关于作者:
互联网行业码农一枚/业余铲屎官/数码影音爱好者/二次元

您必须 登录 才能发表评论!