《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. ...
随机推荐
- 团体程序设计天梯赛L1-019 谁先倒 2017-03-22 17:35 33人阅读 评论(0) 收藏
L1-019. 谁先倒 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳 ...
- 用shell脚本 计算两个数的加减乘除取余
#! /bin/bash # read -p '请输入数:' a //输入 read -p '请输入数:' b echo '$a+$b=' $(( a + b )) //输出 echo '$a-$b= ...
- Reverting back to the R12.1.1 and R12.1.3 Homepage Layout
Reverting back to the 12.1.1 Homepage Layout Set the following profiles: FND: Applications Navigator ...
- ASP.NET Core学习总结(2)
public class ControllerActionInvoker : ResourceInvoker, IActionInvoker 我们知道,ControllerActionInvoker实 ...
- textarea 高度自动
<textarea id="suggest" type="text" name="suggest" class="form- ...
- FIM控制同步用户
C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service\UIShell 这个路径下,你如果懂FIM,可以进去看看 ...
- js实现window.open不被拦截的解决方法汇总
一.问题: 今天在处理页面ajax请求过程中,想实现请求后打开新页面,就想到通过 js window.open 来实现,但是最终都被浏览器拦截了. 二.分析: 在谷歌搜索有没有解决方法,有些说可以通过 ...
- django系列3.1--url路由配置, 正则, 分发include, 分组命名匹配
一.url配置 在django项目中urls.py文件中就是为这个url调用的view(视图)函数之间的映射表,来配置访问的一个url执行什么代码 默认的基本格式: from django.conf. ...
- API接口安全加强设计方法
前面两篇相关文章: <Web Api 内部数据思考 和 利用http缓存优化 Api> <Web Api 端点设计 与 Oauth> 1.开放的接口 这样的接口我们天天都在接触 ...
- “全栈2019”Java多线程第十五章:当后台线程遇到finally
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...