描述: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.

注意问题要求O(1)空间复杂度,注意负数不是回文数。

思路1:将输入整数转换成倒序的一个整数,再比较转换前后的两个数是否相等,但是这样需要额外的空间开销

思路2:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数。

 class Solution {
public:
bool isPalindrome(int x) { //negative number
if(x < )
return false; int len = ;
while(x / len >= )
len *= ; while(x > ) { //get the head and tail number
int left = x / len;
int right = x % ; if(left != right)
return false;
else {
//remove the head and tail number
x = (x % len) / ;
len /= ;
}
} return true;
}
};

LeetCode Problem 9:Palindrome Number回文数的更多相关文章

  1. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  2. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  3. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  4. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  5. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  6. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  7. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  8. 9. Palindrome Number 回文数的判断

    [抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...

  9. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

随机推荐

  1. TP视图命名规则之一

    TP视图命名规则之一   如果觉得目录结构太深,可以通过设置 TMPL_FILE_DEPR 参数来配置简化模板的目录层次,例如设置: 'TMPL_FILE_DEPR'=>'_' 默认的模板文件就 ...

  2. 序列化和反序列化Java 8的时间/日期类

    序列化 假如有 Clock 类: public class Clock { private LocalDate localDate; private LocalTime localTime; priv ...

  3. java之方法的重写

    方法的重写: 1.在子类中可以根据需要对从基类中继承来的方法进行重写. 2.重写的方法和被重写的方法必须具有相同方法名称.参数列表和返回类型. 3.重写方法不能使用比被重写的方法更严格的访问权限. 程 ...

  4. LA 5009 (HDU 3714) Error Curves (三分)

    Error Curves Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatusPr ...

  5. Hive 差集运算

    差集定义:一般地,设A,B是两个集合,由所有属于A且不属于B的元素组成的集合,叫做集合A减集合B(或集合A与集合B之差). 类似地,对于集合A,B,我们把集合{x/x∈A,且x¢B}叫做A与B的差集, ...

  6. Sklearn 中的 CrossValidation 交叉验证

    1. 交叉验证概述 进行模型验证的一个重要目的是要选出一个最合适的模型,对于监督学习而言,我们希望模型对于未知数据的泛化能力强,所以就需要模型验证这一过程来体现不同的模型对于未知数据的表现效果. 最先 ...

  7. 设计模式之代理模式(php实现)

    github地址:https://github.com/ZQCard/design_pattern /** * 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口. * 1.Window ...

  8. Redis之SkipList数据结构

    0.前言 Redis中有序集合zset需要使用skiplist作为存储数据结构, 关于skiplist数据结构描述可以查询wiki, 本文主要介绍Redis实现的skiplist的细节. 1.数据结构 ...

  9. Effective C++:条款22:将成员变量声明为private

    (一)为什么不採用public成员变量 (1)首先,从语法一致性考虑,客户唯一能訪问对象的方法就是通过成员函数,客户不必考虑是否该记住使用小括号(). (2)其次,使用函数能够让我们对成员变量的处理有 ...

  10. ccentos 7下安装php5.6并使用nginx + php-fpm部署多个不同端口网站

    作为一个的勤杂工,近期因公司内部信息化的需求,给新进员工提供基础的知识培训和介绍,也为了给公司内部建立一个沟通交流的平台,百度找了开源的百科系统HDwiki和开源的问答系统Tipask问答系统,蛋痛的 ...