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.

每次区首尾两位进行比较,然后再去掉首尾两位循环。

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cmath>
using namespace std; class Solution {
public:
bool isPalindrome(int x) {
if (x < 0)
{
return false;
}
//数字长度
int len = 0;
int temp = x;
while(temp != 0)
{
temp = temp / 10;
len++;
}
while(x != 0)
{
int bottom = x % 10;
int top = x / (int)pow(10, len - 1);
if (bottom != top)
{
return false;
}
x = (x % (int)pow(10, len - 1)) / 10;
len -= 2;
}
return true;
}
}; int main()
{
Solution s;
cout << s.isPalindrome(123212) << endl;
return 0;
}

最新文章

  1. 深入学习jQuery描述文本内容的3个方法
  2. UWP 禁止Pivot swip 手势
  3. 服务器列表里找不到OracleOraDb10g_home1TNSListener
  4. poj 1028
  5. DX11.2 Tiled Resource Pool
  6. Unity动画
  7. 12个QT基本对话框,以及淡入原理(用定时器把窗口逐渐变成透明)
  8. urllib2中自定义opener
  9. JDK6 下载地址
  10. hdu 2034
  11. boost之词法解析器spirit
  12. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
  13. 【NO.3-1】Jmeter - 在Windows配置HOSTS的方法
  14. tomcat三种启动不同的启动方式
  15. Spring MVC 基础笔记
  16. animation,transform属性
  17. java常见题目总结
  18. 500G !!史上最全的JAVA全套教学视频网盘分享 (JEECG开源社区)
  19. 查询tensorflow中的函数用法
  20. .NET的前世今生与将来

热门文章

  1. $BZOJ1799\ Luogu4127$ 月之谜 数位统计$DP$
  2. CodeIgniter框架多条件搜索查询分页功能解决方案
  3. 洛谷P1832 A+B Problem(再升级) 题解 完全背包方案计数
  4. shiro整合springmvc
  5. 浅谈Java三大特性之封装
  6. AnyDesk免费远程工具
  7. Vue.js项目在apache服务器部署后,刷新404的问题
  8. 解决RabbitMQ远程不能访问的问题
  9. 【转】Twitter算法面试题详解(Java实现)
  10. 小白学 Python 爬虫(39): JavaScript 渲染服务 scrapy-splash 入门