码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 9. Palindrome Number

时间:2017-08-14 01:25:39      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:mon   discuss   ret   一个   microsoft   back   输入   查看   ace   

传送门

Description

Determine whether an integer is a palindrome. Do this without extra space.

思路

题意:判断输入的整数是否是一个回文数,要求不允许使用额外的空间

题解:可以肯定的是小于0以及尾数为0的整数不是回文数,那么剩下的如何在不使用额外的空间判断是否是回文的呢,此题感觉这个不使用额外的空间是为了限制我们不将他转换为字符串。如果是连一个变量的不能使用的话,具体做法可以查看discuss。

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

[LeetCode] 9. Palindrome Number

标签:mon   discuss   ret   一个   microsoft   back   输入   查看   ace   

原文地址:http://www.cnblogs.com/zzy19961112/p/7355516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!