题目:

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. 工控图表控件ProEssentials创建3D柱状图示例代码

    使用ProEssentials可以创建3D柱状图,柱状图的形式包括线框.实体和阴影. 类似于Graph control,3D柱状图只需要YData. Subsets定义沿z轴有多少行,Points定义 ...

  2. 《Python高效开发实战》实战演练——基本视图3

    在完成Django项目和应用的建立后,即可以开始编写网站应用代码,这里通过为注册页面显示一个欢迎标题,来演示Django的路由映射功能. 1)首先在djangosite/app/views.py中建立 ...

  3. docker制作共享jdk的tomcat镜像

    FROM centos:7.4.1708 #挂载宿主机jdk到容器,节省空间 MAINTAINER huqiang:2018/10/12 ENV VERSION=8.5.34 ENV CATALINA ...

  4. PPII打不开 更改I.bat

    http://jingyan.baidu.com/article/3a2f7c2e7d277126afd6118d.html

  5. codeforces 599D Spongebob and Squares

    很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...

  6. angular6项目中使用echarts图表的方法(有一个坑,引用报错)

    1.安装相关依赖(采用的webpack) npm install ecahrts --save npm install ngx-echarts --save 2.angular.json 配置echa ...

  7. Oracle 数字处理函数

    数字处理函数 ① mod(number1,number2) 取余数的函数,比如mod(10,3) = 10/3 = 1. ② round(number,num_ditigs) .trunk(numbe ...

  8. UITableView 优化总结

    最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化UITableView的.加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优 ...

  9. linux 下备份mysql数据库

    今天老板让备份数据库没办法自己折腾吧,下面把折腾的结果总结总结. 数据库备份思路: 1.编写脚本 2.执行脚本  哈哈,是不是很简单,打开冰箱,放入大象,关上.下面我是具体操作. 一.编写脚本 1.设 ...

  10. Linux IO调度方法

    目录 I/O调度的4种算法 I/O调度程序的测试 ionice IO调度器的总体目标是希望让磁头能够总是往一个方向移动,移动到底了再往反方向走,这恰恰就是现实生活中的电梯模型,所以IO调度器也被叫做电 ...