LeetCode Problem 9: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.
注意问题要求O(1)空间复杂度,注意负数不是回文数。
思路1:将输入整数转换成倒序的一个整数,再比较转换前后的两个数是否相等,但是这样需要额外的空间开销
思路2:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数。
class Solution {
public:
bool isPalindrome(int x) { //negative number
if(x < )
return false; int len = ;
while(x / len >= )
len *= ; while(x > ) { //get the head and tail number
int left = x / len;
int right = x % ; if(left != right)
return false;
else {
//remove the head and tail number
x = (x % len) / ;
len /= ;
}
} return true;
}
};
LeetCode Problem 9:Palindrome Number回文数的更多相关文章
- 【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. Some hints: Could neg ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】Palindrome Number(回文数)
这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...
- Palindrome Number 回文数
判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出. ...
- 9. Palindrome Number 回文数的判断
[抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- Netty 中文教程 (二) Hello World !详解
1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...
- Linux网络配置之虚拟网卡的配置(ubuntu 16.04)案例
sudo vim /etc/network/interfaces 标红的名称一定要一致 sudo vim /etc/resolv.conf 配置外网的ip(默认可以不填,系统自己获取)
- [Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization
By setting the strictPropertyInitialization flag in the .tsconfig file, TypeScript will start throwi ...
- 自己亲自写的两本linux资料,免费下载,pdf文档
第一本是我写的韩顺平老师解说的linux视频的笔记,该视频原本有21讲.可是我始终没有找到当中的17.18讲.可是其它部分我感觉及记录的还是蛮认真的.该套视频解说的非常基础,因此我的这本笔记也非常基础 ...
- 微信小程序 的文字复制功能如何实现?
text设置属性 selectable="true" 就可以长按复制了 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有 ...
- Android Exception 17(database or disk is full)
android.database.sqlite.SQLiteFullException: database or disk is full delete some app,or clear cache
- java 学习帮助
java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是我你是如何学习Java的,能不能给点建议?今 天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来 ...
- hdu3415 Max Sum of Max-K-sub-sequence 单调队列
//hdu3415 Max Sum of Max-K-sub-sequence //单调队列 //首先想到了预处理出前缀和利用s[i] - s[j]表示(j,i]段的和 //之后的问题就转换成了求一个 ...
- CentOS 7中mariadb编译安装教程systemctl启动
mariadb做为mysql的替代品 现在centos的新版本yum包已换成mariadb 跟上篇一样只是启动方式改为systemd 安装一些库 yum install gcc gcc-c++ wge ...
- [ci] 构建触发器,实现当gitlab有push动作时候,jenkins自动拉代码.
构建触发器,实现当gitlab有push动作时候,jenkins自动拉代码. 拉取gitlab代码库: jenkins安装git插件 配置gitlab服务器和jenkins服务器身份互信 jenkin ...