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.

非常简洁的c++解决方案:

对于回文数只比较一半

public boolean isPalindrome1(int x) {
if (x == 0) return true;
// in leetcode, negative numbers and numbers with ending zeros
// are not palindrome
if (x < 0 || x % 10 == 0)
return false; // reverse half of the number
// the exit condition is y >= x
// so that overflow is avoided.
int y = 0;
while (y < x) {
y = y * 10 + (x % 10);
if (x == y) // to check numbers with odd digits
return true;
x /= 10;
}
return x == y; // to check numbers with even digits
}

python这个解法应该是前后分别对比:


class Solution:
# @param x, an integer
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False ranger = 1
while x / ranger >= 10:
ranger *= 10 while x:
left = x / ranger
right = x % 10
if left != right:
return False x = (x % ranger) / 10
ranger /= 100 return True

python字符串的解法:

class Solution:
# @param {integer} x
# @return {boolean}
def isPalindrome(self, x):
return str(x)==str(x)[::-1]

leetcode 9 Palindrome Number 回文数的更多相关文章

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

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

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

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

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

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

  4. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

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

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

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

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

  7. Palindrome Number 回文数

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

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

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

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

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

随机推荐

  1. python学习之路前端-jQuery

    jQuery简介      JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safa ...

  2. Rabbitmq集群

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 Rabbitmq集群高 ...

  3. Python3 多线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  4. 这是最好的时光,这是最坏的时光 SNAPSHOT

    好久没动笔了,上次憋了好几天码出的文字扔出去,石沉大海,没惊起半点涟漪.这次真不知道能憋出个什么鬼,索性就让思绪飞扬,飞到哪是哪! --题记 此处应有BGM: 少年锦时 赵雷 1.以后真没有暑假喽 2 ...

  5. Android必知必会-发布开源 Android 项目注意事项

    如果移动端访问不佳,请使用 –> Github版 1. 合理配置 .gitignore 文件 配置 .gitignore 可以排除一些非必要文件和保护保密信息,下面是在项目根目录下 .gitig ...

  6. SpriteKit给游戏弹跳角色添加一个高度标示器

    这是一个类似于跳跃涂鸦的小游戏,主角不断吃能量球得到跳跃能量向更高的地方跳跃,如果图中碰到黑洞就挂了- 在游戏调试过程中如果能实时知道主角的高度就好了,这将有助于程序猿动态的判断游戏胜败逻辑. 你可以 ...

  7. [ExtJS5学习笔记]第二十七节 CMD打包错误 Error C2009: YUI Parse Error (identifier is a reserved word => debugger;)

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/41242993 本文作者:sushengmiyan ------------------ ...

  8. Spark分布式计算和RDD模型研究

    1背景介绍 现今分布式计算框架像MapReduce和Dryad都提供了高层次的原语,使用户不用操心任务分发和错误容忍,非常容易地编写出并行计算程序.然而这些框架都缺乏对分布式内存的抽象和支持,使其在某 ...

  9. velocity map list 数组操作

    Velocity生成模板的时候,经常需要使用到map.list对象,然后遍历输出对象的属性值.当你需要遍历的时候记录遍历的步长的时候,可以使用$velocityCount内置变量进行输出.下面demo ...

  10. C语言中switch case语句可变参实现方法(case 参数 空格...空格 参数 :)

    正常情况下,switch case语句是这么写的: : : ... ;break ; default : ... ;break ; } 接下来说一种不常见的,但是对于多参数有很大的帮助的写法: 先给一 ...