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. gdb调试的基本使用

    GDB调试 启动程序准备调试 GDB yourpram 或者 先输入GDB 然后输入 file yourpram 然后使用run或者r命令开始程序的执行,也可以使用 run parameter将参数传 ...

  2. vuex存储和本地存储(localstorage、sessionstorage)的区别

    1.最重要的区别:vuex存储在内存,localstorage则以文件的方式存储在本地 2.应用场景:vuex用于组件之间的传值,localstorage则主要用于不同页面之间的传值. 3.永久性:当 ...

  3. 01_Struts2概述及环境搭建

    1.Struts2概述: Struts2是一个用来开发MVC应用程序的框架. Struts2提供了web应用程序开发过程中一些常见问题的解决方案; 对用户输入的数据进行合法性验证 统一的布局 可扩展性 ...

  4. Node.js TLS/SSL

    Stability: 3 - Stable 可以使用 require('tls') 来访问这个模块. tls 模块 使用 OpenSSL 来提供传输层(Transport Layer)安全性和(或)安 ...

  5. 虚拟机访问互联网的方法 -- 以RedHat系为例

    在虚拟机的三种网络模式中(Host-Only.桥接.NAT),能够实现虚拟机访问互联网的只有桥接与NAT模式,而Host-only主能实现虚拟机与主机两者间的通信.下面以RedHat系虚拟机系统为例, ...

  6. android注解入门 并来自己写一个框架

    介绍 这里我带大家来学习一下注解 并且用来写下一个模仿xUtils3 中View框架 此框架 可以省略activity或者fragment的 findViewById 或者设置点击事件的烦恼 我正参加 ...

  7. 20160211.CCPP体系详解(0021天)

    程序片段(01):01.指针数组.c+02.动态数组.c 内容概要:指针数组 ///01.指针数组.c #include <stdio.h> #include <stdlib.h&g ...

  8. PLSQL程序编写杂烦数据表信息编写批量排版

    --PLSQL程序编写杂烦数据表信息编写批量排版 SELECT 'cra.' || lower(t.column_name) ||',' FROM dba_tab_columns t WHERE t. ...

  9. 剑指Offer——搜狐畅游笔试题+知识点总结

    剑指Offer--搜狐畅游笔试题+知识点总结 情景回顾 时间:2016.9.24 10:00-12:00 地点:山东省网络环境智能计算技术重点实验室 事件:搜狐畅游笔试   注意事项:要有大局观,该舍 ...

  10. 论Android代码加固的意义和hook

    加固的意义 从安卓2.x版本起,加固技术就逐渐火了起来.最初,只有一些创业型公司涉及加固领域:随着安卓应用的逐渐升温,诸如阿里.腾讯.百度等大型互联网公司逐渐涉及该领域. 那么为什么要对APP进行加固 ...