题目:

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.

代码:

class Solution {
public:
bool isPalindrome(int x)
{
if (x<) return false;
return x==Solution::reverse(x);
}
static int reverse(int x)
{
int ret = ;
while (x)
{
if ( ret>INT_MAX/ || ret<INT_MIN/ ) return ;
ret = ret* + x%;
x = x/;
}
return ret;
}
};

tips:

1. 如果是负数,认为不是回文数

2. 如果是非负数,则求这个数的reverse,如果等于其本身就是回文数,否则就不是。

利用的就是reverse integer这道题(http://www.cnblogs.com/xbf9xbf/p/4554684.html)。

====================================

还有一种解法可以不用reverse,有点儿类似求字符串是否是回文的题目(头尾两个指针往中间夹逼)。

class Solution {
public:
bool isPalindrome(int x)
{
if (x<) return false;
int d = ;
while ( x/d>= ) d = d*;
while ( x> )
{
int high = x/d;
int low = x%;
if (high!=low) return false;
x = x%d/;
d = d/;
}
return true;
}
};

对于这道题的整数来说,就是找到给定整数的最大位数-1,即d。

每次从高位取一个数,从低位取一个数;然后,再把高低位都去掉获得新数(x=x%d/10);这样照比上一次的数少了两位,因此还有d=d/100。

===========================================

第二次过这道题,根据题意提示,用reverse的结果判断是否是palindrome number。

class Solution {
public:
bool isPalindrome(int x)
{
if ( x< ) return false;
int rev = Solution::reverse(x);
return rev==x;
}
static int reverse(int x)
{
vector<int> digits;
int sign = x> ? : -;
int ret = ;
while ( true )
{
int digit = fabs(x%);
if ( INT_MAX/<ret || (INT_MAX/==ret && INT_MAX%<digit) )
{
return ;
}
ret = ret* + digit;
x = x/;
if ( fabs(x)== ) break;
}
return ret*sign;
}
};

【Palindrome Number】cpp的更多相关文章

  1. 【Palindrome Partitioning】cpp

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

  2. 【Valid Number】cpp

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

  3. 【Single Number】cpp

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

  4. 【Letter Combinations of a Phone Number】cpp

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  5. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  6. 【Stirling Number】

    两类Stirling Number的简介与区别(参考自ACdreamer的CSDN) Stirling Number I --- s(n,k):将n个物体排成k个非空循环排列(环)的方法数. 递推式: ...

  7. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  8. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  9. 【system.number】使用说明

    对象:system.number 说明:提供一系列针对数值类型的操作 目录: 方法 返回 说明 system.number.isNumber( number ) [True | False] 检测是否 ...

随机推荐

  1. js学习的一些想法(有一些来自网络)

    javascript开发最佳实践学习 1.给变量和函数命名--变量名和函数名尽量简短 好的变量命名应该是简短易懂的,还有需要避免的陷阱就是在命名中将数值与功能结合. 匈牙利命名法就是一个不错的选择,也 ...

  2. 【ros depthimage_to_laser kinect2】

    kinect2的深度图可以转换成激光来用,使用depthimage_to_laser 这个tf是用来给rviz显示的 1)开启kinect2 rosrun kinect2_bridge kinect2 ...

  3. PHP线程安全和非线程安全有什么区别

    我们先来看一段PHP官网的原话: Which version do I choose? IIS If you are using PHP as FastCGI with IIS you should ...

  4. tp3.2.3自定义全局函数的使用

    全局函数的定义,好处就是我们可以跨文件使用,而且调用方式可以直接调用,十分方便,在这里做个小记录 1.在Application/Home/Common目录下面新建一个名为function.php的文件 ...

  5. python资料汇总

    http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html

  6. IOS 核心动画(Core Animation)

    Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它 能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就 可以实现非常强大的功能. Core ...

  7. JAVA设计模式初探之适配器模式(转)

    1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...

  8. 【51nod1299】监狱逃离(树形DP)

    点此看题面 大致题意: 在一棵树中有\(N\)条边连接\(N+1\)个节点,现在已知这棵树上的\(M\)个节点,要求封住最少的节点,使这\(M\)个节点中的任意一个节点无法到达叶子节点,若能办到输出最 ...

  9. python_61_装饰器4

    import time def timer(func):#timer(test1) func=test1 def deco(): start_time=time.time() func()#run t ...

  10. CUDA常见问题与解答

    源 1.在SDK自带的例子程序中,发现SRC文件珜下有.cpp文件和.cu文件.这两种文件的关系和各自的作用是什么呀? 答:SDK自带例子中的.cpp文件主要是一些CPU端处理,或者是使用CPU计算对 ...