题目:

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. 转 造成ORA-01843 无效的月份的一些原因

  2. C#中MessageBox用法大全(附效果图)<转>

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~&quo ...

  3. 排错:expected unqualified-id before string constant

    一个低级但是不好定位的编译错误,常见的问题是: 1. 语句的 { 括号不匹配. 2. 缺少 : , 特别是类的定义或声明,枚举的定义. 3. 变量名或函数名使用了保留字.

  4. wine使用

    wineqq 不能输入问题winecfg在 wine 设置里,选择函数库添加 riched20, 就行了(原装领先于内建) wineqq 可以输入不能输入中文问题原因:fictx组件缺失 搜狗输入法没 ...

  5. 【数据分析 R语言实战】学习笔记 第八章 方差分析与R实现

    方差分析泛应用于商业.经济.医学.农业等诸多领域的数量分析研究中.例如商业广告宣传方面,广告效果可能会受广告式.地区规模.播放时段.播放频率等多个因素的影响,通过方差分析研究众多因素中,哪些是主要的以 ...

  6. 【MATLAB 从零到进阶】day2 矩阵 数组

    访问矩阵元素 >> A=[1,2,3;4,5,6;7,8,9]; >> x=A(2,3)% 双下标访问 x = 6 >> x=A(2)% 单下标访问 x = 4 单 ...

  7. Drools应用实例

    Drools 实例介绍 Drools编译与运行: 在Drools当中,规则的编译与运行要通过Drools提供的各种API来实现,这些API总体来讲可以分为三类:规则编译.规则收集和规则的执行. Kmo ...

  8. SQL 转换函数

    1.字符串与字符串相加 字符串相加   得到的是拼接成一列的字符串类型 例如 select name+code from car       name是nvarchar  code也是nvarchar ...

  9. Android(java)学习笔记140:常用的对话框

    一.常见对话框属性: 1. AlertDialog.Builder属性  • setTitle: 为对话框设置标题 :• setIcon : 为对话框设置图标:• setMessage: 为对话框设置 ...

  10. build.sbt的定义格式

    一个简单的build.sbt文件内容如下: name := "hello" // 项目名称 organization := "xxx.xxx.xxx" // 组 ...