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. mongo数据删除和游标

    数据删除 db.集合.remove(删除条件,是否只删除一个数据);默认删多条(false)true删除一条db.集合.remove({}) 删除所有元素但集合还在db.集合.drop() 删除集合 ...

  2. Centos 6.5 安装 rar

    wget http://www.rarsoft.com/rar/rarlinux-x64-5.4.0.tar.gz tar -zxvf rarlinux-x64-5.4.0.tar.gz cd rar ...

  3. Android ImgView属性

    ImageView是用于界面上显示图片的控件. 属性 1.为ImageView设置图片 ①android:src="@drawable/img1": src设置图片,默认图片等比例 ...

  4. OpenCV3.1.0中调用MHI(Motion History Images, 运动历史图像)

    写在前边: OpenCV3.0+要想使用MHI,就要现安装扩展模块opencv_contrib.安装方法见:ubuntu 14.04 64位 安装Opencv3.1.0 (包含opencv_contr ...

  5. 智能指针之 unique_ptr

    对于动态申请的内存,C++语言为我们提供了new和delete运算符, 而没有像java一样,提供一个完整的GC机制,因此对于我们申请的动态内存, 我们需要时刻记得释放,且不能重复释放,释放后不能再去 ...

  6. JavaScript的事件、DOM模型、事件流模型以及内置对象详解(三)

    JS中的事件 JS中的事件分类 1.鼠标事件: click/dbclick/mouseover/mouseout 2.HTML事件: onload/onunload/onsubmit/onresize ...

  7. gradle 入门介绍

    gradle 简介 基于Groovy实现的自动化构建工具,比maven好的一点在于不用写复杂的xml文件.使用script就可以. gradle 专业名词 从一个build.gradle 文件开始,b ...

  8. iOS下使状态栏颜色与H5中背景色一致

    iOS 中有的页面也能会内嵌WebView,然后WebView中用H5做了一个导航,而iOS 中状态栏的颜色很难调整的与H5中导航颜色一致.如下图所示: 其实出现这种原因,主要是因为使用16进制颜色, ...

  9. ArrayList、HashMap、HashSet源码总结

    ArrayList: 1. ArrayList是List接口的大小可变数组的实现,此实现是不同步的. 2. ArrayList内部使用类型为Object[]的数组存储元素. 3. ArrayList默 ...

  10. 剑指offer面试题5 从头到尾打印链表(java)

    注:(1)这里体现了java数据结构与C语言的不同之处 (2)栈的操作直接利用stack进行 package com.xsf.SordForOffer; import java.util.Stack; ...