题目:

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. php 01

    PHP 一.了解php 1.什么是php PHP 超文本预处理器 服务器端的脚本语言  是一种被广泛应用的开放源代码的多用途脚本语言  他可以嵌入到html中 尤其适用web开发 2.php在web中 ...

  2. Eucalyptus-利用镜像启动一个Windows Server 2008r2实例

    1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...

  3. use scanner/smb/smb_version

    use scanner/smb/smb_version msf auxiliary(smb_version) > set RHOSTS 172.16.21.170RHOSTS => 172 ...

  4. pysnmp使用

    install yum install python-pysnmp yum install python-pyasn1 or pip install pysnmp pip install pyasn1 ...

  5. java设计模式、框架、架构、平台之间的关系

        设计模式<框架<架构<平台,从复用角度讲,设计模式是代码级复用.框架是模块级复用.架构是系统级复用.平台是企业应用级复用. 1.设计模式 为什么要先说设计模式?因为设计模式在 ...

  6. spark dataframe函数编程

    DataFrame 的函数 Action 操作 1. collect() ,返回值是一个数组,返回dataframe集合所有的行 2. collectAsList() 返回值是一个Java类型的数组, ...

  7. python_74_pickle反序列化

    import pickle def say(name):#序列化时用完会释放,要想反序列化,要重新写上该函数,否则会出错 print('我的高中', name)#可以和之前的序列化函数不同 f=ope ...

  8. python_55_局部和全局变量

    school='Hebut'#school为全局变量 sex='male'#全局变量 names=['Wang Yu','Bai Jingyi','Zhang Yu'] hobby='姑娘' def ...

  9. python_20_列表

    #1 names=["QiZhiguang","DaiYang","HuZhongtao","ZhangDong"] p ...

  10. elasticsearch RestHighLevelClient 使用方法及封装工具

    目录 EsClientRHL 更新日志 开发原因: 使用前你应该具有哪些技能 工具功能范围介绍 工具源码结构介绍 开始使用 未来规划 git地址:https://gitee.com/zxporz/ES ...