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.

题解:

  easy题

Solution 1

 class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
int tmp = x;
while (tmp > ) {
tmp /= ;
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};

Solution 2

 class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
while (x / base > ) {
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};

最新文章

  1. angular.extend用法实例
  2. php语句
  3. MVC 实体字段自定义验证
  4. 浅析初等贪吃蛇AI算法
  5. 仅在TabControl中的Tab中添加右键菜单
  6. 以对象的方式来访问xml数据表(三)
  7. 不要在初始化方法和dealloc方法中使用Accessor Methods
  8. 山东理工大学第七届ACM校赛-字符的变化 分类: 比赛 2015-06-26 10:32 46人阅读 评论(0) 收藏
  9. homework-01 &quot;最大子数组之和&quot;的问题求解过程
  10. 人人网FED CSS编码前端开发规范
  11. ios ColorLUT滤镜
  12. 让你在DOS中任意切换目录
  13. ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能
  14. 基于VC++的网络扫描器设计与实现
  15. 回滚的意义---JDBC事务回滚探究
  16. mac Nginx+CI出现404错误
  17. iOS开发-Autolayout小结
  18. 剑指offer 09变态跳台阶
  19. java 线程池 ExeutorService
  20. 如何在 Linux 上永久挂载一个 Windows 共享

热门文章

  1. Kattis - register 【水】
  2. JVM原理(Java代码编译和执行的整个过程+JVM内存管理及垃圾回收机制)
  3. PHP扩展模块Pecl、Pear以及Perl的区别
  4. Kubernetes 部署Mysql 主从复制集群
  5. Ubuntu 配置NTP Server
  6. Python 时间日历类型
  7. asp.net 5 (mvc 6) 获取网站的物理路径
  8. codeforces 155D 质数
  9. rollingstyle in log4net
  10. Solr 报错:java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody