【LeetCode】009. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
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.
题解:
easy题
Solution 1
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
int tmp = x;
while (tmp > ) {
tmp /= ;
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};
Solution 2
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
int base = ;
while (x / base > ) {
base *= ;
} while (x) {
int head = x / base;
int tail = x % ;
if (head != tail)
return false;
x = (x % base) / ;
base /= ;
} return true;
}
};
【LeetCode】009. Palindrome Number的更多相关文章
- 【LeetCode】9. Palindrome Number (2 solutions)
Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click t ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【leetcode】9. Palindrome Number
题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...
- 【leetcode】 9. palindrome number
@requires_authorization @author johnsondu @create_time 2015.7.13 9:48 @url [palindrome-number](https ...
- 【LeetCode】9 Palindrome Number 回文数判定
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- 寻找最大(小)的K个数
<<编程之美>>一书中提到了寻找最大的K个数的问题,问题可以简单描述为:在长度为N的数组中,寻找第K(K<N)个最大的数.问题的解法涉及到了很多排序算法,对我们理解和运用 ...
- Web前端开发人员和设计师必读文章推荐【系列十】
<Web前端开发人员和设计师必读文章推荐系列十>给大家带来最近两个月发布在<梦想天空>的优秀文章,特别推荐给 Web 开发人员和设计师阅读.梦天空博客关注 前端开发 技术,展示 ...
- RHEL 5 安装gcc
rpm -ivh kernel-headers... rpm -ivh glibc-headers... rpm -ivh glibc-devel... rpm -ivh libgomp.. rpm ...
- CSS 命名里面有大学问
其实迟迟不敢开始写关于样式里布局方面的心得, 因为大多数人眼中,哪需要管那么多,只需要最终效果达到了就行了呗. 然而,即使是如今国内外顶级大牛也不敢说自己是个优秀的 CSS 工程师. 一般大公司都是 ...
- 【Head First Servlets and JSP】笔记12:URL重写
1.会话管理的核心在于“交换会话ID”,来回传递cookie是最简单的方法,容器通过客户端发来的JSSESIONID查找匹配的对话. 2.如果浏览器禁用了cookie,那就意味着浏览器将忽略响应首部中 ...
- python的语法错误总结
1.keyerror一般是你使用字典里不存在的key产生的错误. 2.TypeError一般是使用的数据类型不符合要求 join函数要求a都是string
- 20145240《Java程序设计》第一周学习总结
20145240 <Java程序设计>第一周学习总结 教材学习内容总结 第一周学习Java首先从最基本的下载.配置环境.了解基本人机命令行方式等基础知识,以及编写简单的"Hell ...
- yum 源配置
在 /etc/yum.repos.d 下建立一个 .repo 文件 vim yum.repo [cd] name=cd baseurl=file:///run/media/root/RHEL-7.0 ...
- linux 查看各目录(文件夹)下文件大小
# 显示总大小(/下全部文件占用大小) du -sh /* | sort -nr # 显示各文件夹的大小(当前文件夹下各文件夹的大小) du --max-depth=1
- 【bzoj1345】[Baltic2007]序列问题Sequence
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1345 因为合并的花费是较大数的权值,所以每个数肯定是和附近的小数合并完后才与大数合并, ...