题目:

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. ZJOI2017 day2 T2 线段树 想法题

    考完D2发现自己简直zz了...花式扔基本分 首先这道题有个显然的套路:树上一些点到一个定点的距离和=这些点深度和+点数*定点深度和-2*lca深度和 ——上一次见这个套路是LNOI2014,上次做的 ...

  2. siege官方文档(译)(二)

    WHY DO I NEED IT? Siege was written for both web developers and web systems administrators. siege是为了 ...

  3. centos设置ssh免密码登陆

    准备工作:    1.确认本机sshd的配置文件(需要root权限) $ gedit /etc/ssh/sshd_config 找到以下内容,并去掉注释符”#“ RSAAuthentication y ...

  4. C++使用ADO连接数据库及其实例

    读写数据库的技术很多,现在多用ADO.ADO以COM方式提供,所以它的很多行为遵循COM规范.首先,要引入ADO的COM文件,它的位置一般在"C:/Program Files/Common ...

  5. C. Hamburgers

    Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...

  6. iOS  UDP 广播 AsyncSocket 用法

    因为业务需要,需要用广播发送一个字段,在iOS开发中,用到了AsynSocket. 1.定义一个属性,负责发送和接受数据 #define YX_Local_Host @"255.255.25 ...

  7. setuid

    -r-s--x--x   #s就是setuid,仅可用在二进制文件,对目录设置无效

  8. UVA 11324 The Largest Clique (强连通分量,dp)

    给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...

  9. map最基本操作

    #include<iostream> #include<map> using namespace std; int main() { /*map<int,char> ...

  10. canvas 在视频中的用法

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...