题目:

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.

分析:

该题目是判断一个整型数据是否为回文数,我们见过对字符串判断是否为回文串,条件反射即是利用相同的思想,将整型数据转换为字符串。但是再看题目要求是不允许占用额外的空间,也就是说我们不能分配字符串来存储这个数。
换个思路,回文数也就是类似于1、121、1221这样的数据,负数显然不是回文的。
也就是说,我们可以从两个方向最高位 、最低位 依次判断是否相同。

AC代码:

class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false; int size = 0 , tmp = x;
while(tmp != 0 )
{
tmp /= 10;
size++;
}//while int p = x , q = x , i , j;
for(i=1 ,j=size ; i<j ; i++ , j--)
{
int a = pow(10 , j-1);
if(p/a != q%10)
{
return false;
}else{
p %= a;
q /= 10;
}//else
}//for
return true;
}
};

LeetCode(9)Palindrome Number的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...

  3. LeetCode(202) Happy Number

    题目 Write an algorithm to determine if a number is "happy". A happy number is a number defi ...

  4. LeetCode(131)Palindrome Partitioning

    题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  5. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  6. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  7. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  8. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  9. LeetCode(136) Single Number

    题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...

随机推荐

  1. SpirngMVC-JSON

    Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,如下: 配置json转换器 在注解适配器中加入messa ...

  2. morphia(5)-删除

    @Test public void delete() throws Exception { final Query<Employee> query = datastore.createQu ...

  3. linux下curl get方法传递参数

    例如 url 为 http://mywebsite.com/index.php?a=1&b=2&c=3 web形式下访问url地址,使用$_GET是可以获取到所有的参数 然而在linu ...

  4. HDU 3359 高斯消元模板题,

    http://acm.hdu.edu.cn/showproblem.php?pid=3359 题目的意思是,由矩阵A生成矩阵B的方法是: 以a[i][j]为中心的,哈曼顿距离不大于dis的数字的总和 ...

  5. msyql 死锁

    1.使用 show processlist; 查询当前进程; 找到Command 状态是query 并且Time 时间很长的id kill掉即可 2.select * from information ...

  6. 2189 数字三角形W

    2189 数字三角形W 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold       题目描述 Description 数字三角形要求走到最后mod 100最大 输入描述 ...

  7. Android使用MediaRecorder和Camera实现视频录制及播放功能整理

    转载请注明出处:http://blog.csdn.net/woshizisezise/article/details/51878566 这两天产品经理向我丢来一个新需求,需要在项目里添加一个视频录制的 ...

  8. PHP 常用的无限遍历方法

    一.根据父节点ID循环遍历其下所有子节点 /** * @name 根据id递归某个父类节点下的所有分类节点 * @author tbj * @date 2015-12-19 */ public fun ...

  9. (四)maven之查找jar包坐标,选择jar包版本

    ①    先访问http://www.mvnrepository.com/  ,这个地址是maven的公共库. ②   以spring core的jar包为例.在页面的最上方的中间,输入spring ...

  10. 关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)

    本人主要从事网络安全产品的测试,由于一些产品功能在后期稳定后每个版本的迭代仍需要投入大量的时间和精力去测试,所以近期计划逐步的去了解自动化测试的一些内容来节省和解放一些资源.由于自己并没有什么编码基础 ...