《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. ...
随机推荐
- 20169202 2016-2017-2《Windows攻击》
Windows攻击 实验要求:使用Metaspoit攻击MS08-067,提交正确得到远程Shell的截图,加上自己的学号水印 (1):MS08-067远程溢出漏洞描述 MS08-067漏洞的全称为& ...
- cordova/webapp/html5 app 用corsswalk替换内核,优化安卓webview
Crosswalk与WebView的不同 为什么要用corsswalk?由于cordova应用在安卓上运行的时候,都是调用的手机webview,而在不同的安卓机.不同版本的系统上,webview的性能 ...
- 二段Linq Groupby操作
var messages = list.GroupBy(p=>p.RefOrderNo,(k,v)=> new {OrderNo = k,SkuInfo = v}) .Select(p = ...
- vue+elementui后台管理快捷代码片段
Form <el-form labelPosition="right" labelWidth="10%" size="small" : ...
- View Pi's Status on WebBrowser
1. install php and cgi support sudo apt-get install php5-common sudo apt-get install php5-cgi sudo a ...
- 原生态在Hadoop上运行Java程序
第一种:原生态运行jar包1,利用eclipse编写Map-Reduce方法,一般引入Hadoop-core-1.1.2.jar.注意这里eclipse里没有安装hadoop的插件,只是引入其匝包,该 ...
- 在一般处理程序里面读写session
1.引用命名空间 using System.Web.SessionState; 2.继承IRequiresSessionState接口 3.利用httpcontext类读写即可 context.ses ...
- window系统JAVA开发环境的搭建
1.java JSK工具包安装教程http://www.runoob.com/java/java-environment-setup.html 2.Eclipase编辑器安装包教程 http://ww ...
- C++ windows下共享内存
转载:https://blog.csdn.net/tojohnonly/article/details/70246965 共享内存 (也叫内存映射文件) 主要是通过映射机制实现的 , Windows ...
- “全栈2019”Java异常第九章:throws关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...