蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目
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]的更多相关文章
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 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. ...
- 蜗牛慢慢爬 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 ...
随机推荐
- Scala中=>的用法
1. 表示函数的类型(Function Type) 例如: def double(x: Int): Int = x*2 函数double的类型就是 (x: Int) => Int 或者 Int ...
- 使用bootstrap-select控件 搜索栏键入关键字动态获取后台数据
bootstrap-select开源地址:https://github.com/silviomoreto/bootstrap-select bootstrap-select使用示例:http://si ...
- 【BZOJ1053】[HAOI2007]反素数
[BZOJ1053][HAOI2007]反素数 题面 bzoj 洛谷 题解 可以从反素数的定义看出小于等于\(x\)的最大反素数一定是约数个数最多且最小的那个 可以枚举所有的质因数来求反素数,但还是跑 ...
- [算法]用JAVA实现快速排序
问题描述: 实现对数组的普通快速排序与随机快速排序实验要求: (1)实现上述两个算法 (2)统计算法的运行时间 (3)分析性能差异,作出总结 代码: 一:普通快速排序 public class Qui ...
- CSS3新增特性详解(一)
注:由于CSS3的新特性较多,所以分两篇博文来说明.第一篇主要包括新的选择器.文字及块阴影.多背景图.颜色渐变.圆角等.第二篇主要细说CSS3的各种动画效果,如:旋转.移动.缩放等,还包括图标字体的应 ...
- centos7搭建ANT+jmeter+jenkins接口测试自动化环境
一.环境准备 因为用到了jmeter和apache-tomcat,centos7必须要有java环境,所以配置jdk和apache-tomcat什么的,就不多说了,自行操作 帮你们偷懒: ant下载地 ...
- Unity优化方向——优化Unity游戏中的垃圾回收(译)
介绍 当我们的游戏运行时,它使用内存来存储数据.当不再需要该数据时,存储该数据的内存将被释放,以便可以重用.垃圾是用来存储数据但不再使用的内存的术语.垃圾回收是该内存再次可用以进行重用的进程的名称. ...
- elasticsearch(全文检索)的介绍和安装
ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...
- move.js运动插件
move.js 运动插件是一款针对元素动画效果的插件.可以运用此插件制作出各类元素效果. 插件GitHub地址:https://github.com/visionmedia/move.js 下面整理学 ...
- MAVEN项目导入src/test/java项目报错
转载博客:https://blog.csdn.net/gengjianchun/article/details/78679036 https://blog.csdn.net/jsloveyou/ ...