题目:

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. __str__,__repr__,__format__

    __str__,__repr__ __str__:控制返回值,并且返回值必须是str类型,否则报错 __repr__:控制返回值并且返回值必须是str类型,否则报错 __repr__是__str__的 ...

  2. 解决 mac zsh 所有命令失效

    上面的没啥用, 直接看分割线吧, 上面的是第一次遇到这个问题, 没有解决.. zsh: command not found: 参考: https://www.jiloc.com/43492.html ...

  3. CVE-2017-3248——WebLogic反序列化漏洞利用工具

    著名的web中间件WebLogic被曝出之前的反序列化安全漏洞补丁存在绕过安全风险,用户更新补丁后,仍然存在被绕过成功执行远程命令攻击的情况,安全风险高,Oracle官方及时发布了最新补丁,修复了该漏 ...

  4. JetSpeed2因dom4j包冲突导致PSML页面文件数据丢失

    使用JetSpeed2进行二次开发时突然出现在保存Portlet配置信息时出现PSML页面文件数据丢失的情况,几经测试,最终发现是因为Portlet中的dom4j.jar与jetspeed应用中的do ...

  5. gdb手册

    摘自:https://github.com/hellogcc/100-gdb-tips/blob/master/src/quit-gdb-silently.md. 我只是摘抄我平时没注意到的,或者我认 ...

  6. Unity Shader入门精要学习笔记 - 第14章非真实感渲染

    转载自 冯乐乐的 <Unity Shader 入门精要> 尽管游戏渲染一般都是以照相写实主义作为主要目标,但也有许多游戏使用了非真实感渲染(NPR)的方法来渲染游戏画面.非真实感渲染的一个 ...

  7. Spring源码:Spring IoC容器加载过程(2)

    Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...

  8. STL使用迭代器逆向删除

    网上有很多这种例子: void erase(vector<int> &v) { for(vector<int>::reverse_iterator ri=v.rbegi ...

  9. babel-loader7和babel8版本的问题

    根据官网https://www.npmjs.com/package/babel-loader要对应版本 一.babel7.X版本 1.要安装的包  第1套包:npm i babel-core babe ...

  10. 声明已被否决 VS C++

    error C4996声明已被否决,不止一次碰到这个问题,在这里必须mark一下! 尝试这个1.Project Properties > Configuration Properties > ...