《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫《leetbook》的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看
地址:https://github.com/hk029/leetcode
这个是书的地址:https://hk029.gitbooks.io/leetbook/
009. Palindrome Number[E]
问题:
Determine whether an integer is a palindrome. Do this without extra space.
思路
这里说不用额外的空间意思是不用O(n)的空间,O(1)的还是可以用的,不然循环都不好写。。
思路1
简单的思路 就是把数字逆转,然后判断逆转后的数字跟原来数字是不是一样的。
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0) return false;
int r=0,t;
t = x;
while(t != 0)
{
r =r*10 + t%10;
t /=10;
}
return r == x;
}
};
思路2
但是其实,不用把数字逆转完再判断,因为如果是回文数字,那么只要逆转一半看是否满足回文条件就行了。
设新数为r,原来数为x,每次:
x = x/10,r = r*10 + x%10;
如何到一半停止?
- 如果x <= r时可以停止了(至少到了一半)
- 如果是偶数长度,并且是回文,那么刚好可以到x == r
- 如果为奇数长度,并且是回文,那么x < r,这时候r刚好比x多一位数
停止后判断是否是回文
- 如果是偶数长度,很简单判断 r == x
- 如果是奇数长度,要判断 r/10 == x
注意:这里有一些小问题。
当尾数为0的情况。尾数为0会导致r的增长少1位数(因为0*10 = 0)。
比如:110不是回文,最后停止r = 1 x =1 但是 r == x
当数字小于0的时候,也是不满足回文的条件的。
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0 || (x != 0 && x %10 ==0)) return false;
int r = 0;
while(x > r)
{
r =r*10 + x%10;
x /=10;
}
return (r == x) || (r/10 == x);
}
};
《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字的更多相关文章
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- 从0开始的LeetCode生活—9. Palindrome Number(回文数)
题目大意: 判断输入的数字是不是回文数.所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字.负数不会是回文数. 解题思路: 思路一:如果这个数是负数,则返回false,否则用一 ...
- [LeetCode]9. Palindrome Number判断回文数字
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...
- [LeetCode] 409. Longest Palindrome 最长回文
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
随机推荐
- [转]快速入门系列--WebAPI--01基础
本文转自:http://www.cnblogs.com/wanliwang01/p/aspnet_webapi_base01.html ASP.NET MVC和WebAPI已经是.NET Web部分的 ...
- B-spline Curves 学习前言与动机(1)
B-spline Curves 学习之前言 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关的翻译学习. (原来博客网址:http:// ...
- 18-11-2 Scrum Meeting 5
1. 会议照片 2. 工作记录 - 昨天完成工作 1 把数据导入数据库 2 中译英选择题和英译中选择题的查询接口 - 今日计划工作 1 配置页面 2 实现中译英选择题和英译中选择题的查询接口 3 整理 ...
- Tweak和app交互方案【进程通信】
Core Foundation DEMO:Tweak端: CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCent ...
- linux服务器下配置多tomcat
车辆交易用的系统模块,正在做.老板要看看,以便车城那边的人提出意见.于是在服务器上再次增加一个tomcat. 以前是配置过的,配置过程其实很简单,这次太大意了,找了半天问题. 首先是拷贝一个tomca ...
- css transition & animation
transition 支持:IE10+ img{ transition: 1s 1s height ease; } transition-property: 属性transition-duration ...
- C#设计模式(23种模式)
https://www.cnblogs.com/abcdwxc/archive/2007/10/30/942834.html
- Polynomial ( Arithmetic and Algebra) CGAL 4.13 -User Manual
1 Fundamentals A polynomial is either zero, or can be written as the sum of one or more non-zero ter ...
- LightOJ 1197 LightOJ 1197(大区间素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进 ...
- Javascript实例 -- 计时器, 搜索框,selected联动
计时器: <body> <input type="text" id="i1"> <input type="button& ...