题目

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.

翻译

判断数字是否是回文的 不能使用额外空间

Hints

Related Topics: Math

不能转换成字符串 也不要用数字倒置(leetcode 7)

可以利用数字倒置时用到的方法

代码

Java

class Solution {
public boolean isPalindrome(int x) {
if (x<0 || (x!=0 && x%10==0)) return false;
int result = 0;
while (x>result){
result = result*10 + x%10;
x = x/10;
}
return (x==result || x==result/10);
}
}

Python

class Solution(object):
def isPalindrome(self, x):
if x<0 or (x!=0 and x%10==0):
return False
result = 0
while x>result:
result = result*10 + x%10
x = x/10
return (x==result or x==result/10)

蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  2. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  3. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  4. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  5. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  9. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

随机推荐

  1. 20155207王雪纯 Exp2 后门原理与实践

    20155207王雪纯 Exp2 后门原理与实践 实验步骤 一.windows获取Linux shell Windows:使用 ipconfig 命令查看当前机器IP地址. 进入ncat所在文件地址, ...

  2. 20155211 课下测试ch12补做

    20155211 课下测试ch12补做 有关线程图,下面说法正确的是() A.图的原点表示没有任何线程完成一条指令的初始状态 B.向右向上是合法的转换 C.向左向下是合法的转换 D.对角线是合法的转换 ...

  3. spark-submit python 程序,"/home/.python-eggs" permission denied 问题解决

    问题描述,spark-submit 用 yarn 模式提交一个python 脚本运行程序,运行到需要分布式的部分,即map/mapPartition等等RDD的时候,或者actor RDD的时候,报错 ...

  4. Wannafly挑战赛18B 随机数

    Wannafly挑战赛18B 随机数 设\(f_i\)表示生成\(i\)个数有奇数个1的概率. 那么显而易见的递推式:\(f_i=p(1-f_{i-1})+(1-p)f_{i-1}=(1-2p)f_{ ...

  5. GNS3 jungle newsfeed 隐藏

    windows 7 windows 8.1 1.开始---运行 输入(没有引号):“%appdata%” 2.修改---GNS3/gns3_gui.ini 的两行参数 "default_lo ...

  6. node.js学习笔记——前序

    一.什么是node.js 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一 ...

  7. Sqlite数据库sqlite3命令小记

    SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 开始 启动sqlite3程序,仅仅需 ...

  8. Android——调用高德地图API前期准备

    1.登陆高德开放平台注册账号http://lbs.amap.com/ 2.创建自己的应用并且添加新key 获取发布版安全码获取方法: 在AndroidStudio的Terminal中编译: 输入如下图 ...

  9. 关于Netty的学习前总结

    摘要 前段时间一直在学习netty因为工作忙的原因没有写一个学习的总结,今天抽个空先把总结写了吧.事先声明,本文不会详细的介绍每一个部分不过每个部分都会附上讲解详细的url.本文只是为了解释通Nett ...

  10. numpy技巧

    矩阵旋转: mat_array = np.array([[1,2],[4,3]]) rotate_90 = np.zeros((2,2), np.uint8) rotate_180 = np.zero ...