Palindrome Number

来源:互联网 发布:怎么把备份的数据恢复 编辑:程序博客网 时间:2024/05/16 06:32

题目:

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

分析: 数字x对称,那么x的前半部分和后半部分的反转相同,只需求出一半即可,不需考虑溢出问题。

代码:

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


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去韩国留学手机怎么办 韩国办无线网怎么办 淘宝卖家被骗怎么办 淘宝买软件被骗怎么办 被淘宝店诈骗怎么办? 支付宝被骗2000怎么办 给私人打款后不发货怎么办 毕业证寄丢了怎么办 微商下单返现被骗一千四怎么办 淘宝买东西卡里多扣钱怎么办 付款了卖家不发货怎么办 淘宝客服不解决问题怎么办 淘宝未付款订单怎么办 淘宝被限制购买怎么办 苹果官换机维修过怎么办 iphone x官换机坏了怎么办 小娃不要大人睡怎么办? 深度睡眠太少怎么办 踏板摩托车淹缸怎么办 电喷摩托车淹缸怎么办 踏板摩托不过油怎么办 火花塞被汽油淹怎么办 踏板车淹缸了怎么办 电喷汽车淹缸怎么办 踏板摩托车粘缸怎么办 鬼火打不着火怎么办 踏板摩托车没电怎么办 买摩托车被骗了怎么办 鬼火电瓶坏了怎么办 摩托闷油了怎么办 鬼火发不着火怎么办 淘宝买东西发错货了怎么办 咸鱼售假处罚怎么办 趣头条登录不上怎么办 淘宝集运超租怎么办 淘宝发货多发了怎么办 小学生数学学不好怎么办 小学生不爱学英语怎么办 小学生基础知识没学好怎么办 小学生应用题学不明白怎么办 不提供纸质发票怎么办