博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Palindrom Number
阅读量:4185 次
发布时间:2019-05-26

本文共 1543 字,大约阅读时间需要 5 分钟。

// Author : yqtao// Date   : 2016-7-3// Email  : yqtao@whu.edu.cn/************************************************************************************ 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.**********************************************************************************/#include
using namespace std;//Solutin 1bool isPalindrome(int x) { if (x < 0 || (x != 0 && x % 10 == 0)) {
return false; } long long result = 0; //using long long it can not be overflow int temp = x; while (temp) //revere the num; {
result = result * 10 + temp % 10; temp /= 10; if (result>INT_MAX) //if overflow ,it must be return false return false; } return x ==result;}//Solutin 2 bool isPalindrome1(int x) { if (x < 0 || (x != 0 && x % 10 == 0)) {
return false; } int half = 0; while (x > half) { half = half * 10 + x % 10; x /= 10; } if (half == x || half / 10 == x) {
return true; } return false;}int main(){ cout << boolalpha << isPalindrome(121) << endl;}

转载地址:http://cpdoi.baihongyu.com/

你可能感兴趣的文章
豆瓣爱问共享资料插件发布啦
查看>>
kermit的安装和配置
查看>>
linux中cat命令使用详解
查看>>
java中的异常机制
查看>>
商务智能-基本方法-数据钻取
查看>>
openstack ice resize 详解(三)
查看>>
事务与锁(转)
查看>>
Namenode HA原理详解(脑裂)
查看>>
Differences between VMware FT and HA(转)
查看>>
Cloud Prizefight: OpenStack vs. VMware(转)
查看>>
亚马逊Auto Scaling
查看>>
openstack-instance-high-availability-Evacuate
查看>>
evacuate-instance-automatically
查看>>
pycharm常用设置(keymap设置及eclipse常用快捷键总结)
查看>>
关于在openstack的环境变量.bashrc自定自己简化命令
查看>>
Openstack Heat Project介绍(转)
查看>>
How to Perform an Upgrade from Icehouse to Juno(ice升级到juno)
查看>>
高扩展性网站的50条原则(转)-思维导图
查看>>
解决openstack novnc一段时间后自动挂断登录不上问题,novncproxy dead but pid file exists
查看>>
构建OpenStack的云基础架构:ManageIQ(转)
查看>>