蜗牛慢慢爬 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 ...
随机推荐
- 【python3】爬取鼠绘汉化的海贼王漫画
特别说明: 因为早些时候鼠绘的接口调整,之前的代码已经不能用了. 正好最近在学习scrapy,于是重新写了一个,项目放在github https://github.com/TurboWay/ishu ...
- 10-[CSS]-盒模型:border,padding,margin
1.CSS盒子模型 HTML文档中的每个元素都被描绘成矩形盒子,这些矩形盒子通过一个模型来描述其占用空间,这个模型称为盒子模型. 盒子模型通过四个边界来描述:margin(外边距),border(边框 ...
- iOS开发-通过正则表达式进行各种判断银行卡,车牌号,邮箱地址,QQ,身份证,全字母,仅输入字母或数字同时包含大小写字母和数字,仅能输入中文等
/* * 验证银行卡号是否正确 * 车牌号验证 * 检验邮箱地址是否正确 * 手机号中间四位密文显示 * 判断QQ号是否正确(5-11位) * 判断身份证号是否正确(如末位为字母请用“x” ...
- angularJs中缓存数据,免去重复发起请求的几种写法
带缓存处理的两种写法 过程:点击button触发load()方法,请求数据成后显示到页面中.如果已经请求过则从缓存中读取. 在线浏览 写法1: function demo(){ if (demo.ca ...
- kali base64命令的使用
1.启动终端 2.输入base64 3.输入需要编码的字符串 4,CTRL+D,结束输入,在屏幕上回显输出结果
- SQL Server 任务调度
SQL Server 内部集成了一个专用的操作系统,叫做SQLOS,处于SQL Server和Windows的中间层.SQLOS是一个协同式的多任务调度系统,使用非抢占式争用资源,用于管理线程调度.I ...
- 【JUC源码解析】DelayQueue
简介 基于优先级队列,以过期时间作为排序的基准,剩余时间最少的元素排在队首.只有过期的元素才能出队,在此之前,线程等待. 源码解析 属性 private final transient Reentra ...
- requests.get()解析
1.requests.get(url, params=None, headers=None, cookies=None, auth=None, timeout=None) Sends a GET re ...
- GoLang入门(一)
1.Go语言的简述 Go语言可能大家对它并不是很了解,因为它相对其他语言来说实在是太年轻了,从创建到现在仅仅十年,09年,谷歌团队初设该想法,到实现也就是10年时间,对于go语言,它不是万能的,每种语 ...
- java学习(四)修饰符、运算符、循环结构、分支结构
修饰符 一般是放在定义类,方法,变量的最前端 访问控制修饰符 修饰符 当前类 同一包内 子孙类 其他包 public Y Y Y Y protected Y Y Y N default Y Y N N ...