描述: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. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  2. java web程序中项目名的更改(http://localhost:8080/)后面的名字

    在MyEclipse中的Web项目,如果想另换名称(Context-root)进行发布,可以在项目的属性中进行设置.设置路径如下: 右击项目XX(或选中项目XX,按快捷键Alt+Enter), 打开项 ...

  3. JUC同步器框架

    The java.util.concurrent Synchronizer Framework 前提 AQS(java.util.concurrent.locks.AbstractQueuedSync ...

  4. 时间见证着—eternal life

    上帝并不是这么公平的爱每个人,祂会多爱那些在困难.痛苦.悔恨中的人一点点.因为在那个当下,他们比一般人更需要知道上帝对他们的爱.   开通空间:http://imgcache.qq.com/qzone ...

  5. JavaScriptl 类数组转换为数组

    slice和Array.form方法,具体见示例代码: <!DOCTYPE html> <html lang="zh"> <head> < ...

  6. Java模式开发之责任链模式

    Java模式开发之责任链模式 从击鼓传花谈起 击鼓传花是一种热闹而又紧张的饮酒游戏.在酒宴上宾客依次坐定位置,由一人击鼓.击鼓的地方与传花的地方是分开的.以示公正. 開始击鼓时,花束就開始依次传递,鼓 ...

  7. 更改字段、添加字段脚本以及sql回滚

    --修改字段名称 EXEC sp_rename '[dbo].[SysMenu].[Type]', 'Position', 'COLUMN' --添加字段 alter table [dbo].[Age ...

  8. margin: 0px auto; center 行类 块级

    <html> <head> <title> biaoti </title> </head> <body style="bor ...

  9. 工作总结 "2017年8月11日" 转换为datatime

    string strr = "2017年8月11日"; Console.WriteLine((Convert.ToDateTime(strr)).ToString("yy ...

  10. 【Akka】在并发程序中使用Future

    引言 在Akka中, 一个Future是用来获取某个并发操作的结果的数据结构.这个操作一般是由Actor运行或由Dispatcher直接运行的. 这个结果能够以同步(堵塞)或异步(非堵塞)的方式訪问. ...