题目:

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编码转换相关

    iconv (PHP 4 >= 4.0.5, PHP 5, PHP 7) iconv — 字符串按要求的字符编码来转换 string iconv ( string $in_charset , s ...

  2. C#启动或停止 计算机中“服务”

    第一.要添加一个引用System.ServiceProcess 第二.要在程序中使用命名空间ServiceProcess 代码片段: using System.ServiceProcess; Serv ...

  3. linux 命令——42 kill (转)

    Linux 中的kill命令用来终止指定的进程(terminate a process)的运行,是Linux下进程管理的常用命令.通常,终止一个前台进程可以 使用Ctrl+C键,但是,对于一个后台进程 ...

  4. 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法

    遇见这个问题是在使用了MD5加密算法后报错的,可能的原因如下: 1.FIPS不兼容MD5,此时需要修改注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\C ...

  5. vs code vim

    很多初学者启动vim后,不知道怎么输入字符:按了半天字母,结果屏幕还是空的. vim和记事本或WORD不一样,不是一打开后就可以输入文字,此时它处于正常模式. vim一共有4个模式: 正常模式 (No ...

  6. Java代码工具箱之解析单行单列简单Excel

    1. 使用开源工具 jxl.jar 2. 功能:解析常规Excel.xls格式测试可行,xlsx未测试.Excel测试格式为常规类似table这种简单布局文件.第一行为标题,后面行为内容.代码 可正确 ...

  7. Ubuntu使用问题解决办法

    http://blog.csdn.net/ll_0520/article/details/6077913

  8. java算法面试题:有数组a[n],用java代码将数组元素顺序颠倒

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.List; ...

  9. CSS的垂直居中和水平居中总结

    内联元素居中方案 水平居中设置: 行内元素 设置 text-align:center: Flex布局 设置display:flex;justify-content:center;(灵活运用) 垂直居中 ...

  10. org.hibernate.hql.internal.ast.QuerysyntaxException:user is not mapped [from User where user_code=? and user_password=?]

    初学者,一个很低级的错误吧! 找不到映射,最后发现没把类的Hibernate映射文件 添加到Hibernate核心配置文件中去,所以报了这个异常! 在核心文件中添加映射   <mapping r ...