【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 ...
随机推荐
- HTML系列(1)简介
开始整理html的知识. (1)HTML HTML 是用来描述网页的一种语言. 1.HTML指的是超文本标记语言: HyperText Markup Language 2.HTML不是 ...
- arcgis flex aqi 3大util
第一:webMapUtil 主要用来根据id或者json创建map,跟webmap相关 第二:GeometryUtil 主要用来计算面积,长度,还有判断是否相交等,跟geometry相关. 第三:We ...
- 【HackerRank】Encryption
One classic method for composing secret messages is called a square code. The spaces are removed fr ...
- Java zip 压缩 文件夹删除,移动,重命名,复制
FileUtil.java import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.ut ...
- CSS3 3D旋转按钮对话框
在线演示 本地下载
- Linux设置默认启动命令行,而不是图形界面
无论是作为上线的服务器还是开发中的虚拟机来说,都是没有必要启动图形界面的,而图形界面占用的内存还是很大的.枫竹梦本文就介绍如何设置来默认启动命令行. 其实就是设置系统的启动级别. CentOS的设置 ...
- python爬虫之html解析Beautifulsoup和Xpath
Beautiifulsoup Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.BeautifulSoup 用来解析 HTML 比较简 ...
- Linux 修改DNS解决 Could not retrieve mirrorlist" 报错
CentOS yum有时出现“Could not retrieve mirrorlist ”的解决办法——resolv.conf的配置 或者IP配置文件上写入 缺少DNS引起的问题1. 无法识别域名 ...
- 【P2014】选课(树状DP)
蒟蒻的第二道树形DP,话说看了这个题的正常做法之后一脸蒙,森林转二叉树??什么诡异的操作,蒟蒻完全没明白那个原理是啥...可能是当初没好好学吧..不管了,索性直接DP. 不难看出,这个题的DP方程和刚 ...
- 虚拟机CentOS6.5网络配置
不得不说 6.5比7.0麻烦了许多.. 编辑ifcfg配置文件 vi /etc/sysconfig/network-script/ifcfg-eth0 内容如下 DEVICE=eth0 HWADDR ...