Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
门心叼龙
LeetCodeAnimation
提交
dc285aa7
L
LeetCodeAnimation
项目概览
门心叼龙
/
LeetCodeAnimation
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
LeetCodeAnimation
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
dc285aa7
编写于
5月 27, 2019
作者:
M
misterbooo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
LeetCode第9号问题:回文数
上级
79fd9bbf
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
148 addition
and
0 deletion
+148
-0
Readme.md
Readme.md
+1
-0
notes/LeetCode第9号问题:回文数.md
notes/LeetCode第9号问题:回文数.md
+147
-0
未找到文件。
Readme.md
浏览文件 @
dc285aa7
...
...
@@ -18,6 +18,7 @@
| 1 |
[
两数之和
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第1号问题:两数之和.md
)
|
| 2 |
[
两数相加
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第2号问题:两数相加.md
)
|
| 3 |
[
无重复字符的最长子串
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第3号问题:无重复字符的最长子串.md
)
|
| 9 |
[
回文数
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第9号问题:回文数.md
)
|
| 15 |
[
三数之和
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第15号问题:三数之和.md
)
|
| 19 |
[
删除链表的倒数第 N 个节点
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第19号问题:删除链表的倒数第N个节点.md
)
|
| 20 |
[
有效的括号
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第20号问题:有效的括号.md
)
|
...
...
notes/LeetCode第9号问题:回文数.md
0 → 100644
浏览文件 @
dc285aa7
# LeetCode 第 9 号问题:回文数
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
题目来源于 LeetCode 第 9 号问题:回文数。题目难度为 Easy,目前通过率为 56.0%。
## 题目描述
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
**示例 1:**
```
输入: 121
输出: true
```
**示例 2:**
```
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
```
**示例 3:**
```
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
```
**进阶:**
你能不将整数转为字符串来解决这个问题吗?
## 题目解析
### 解法一:普通解法
最好理解的一种解法就是先将
**整数转为字符串**
,然后将字符串分割为数组,只需要循环数组的一半长度进行判断对应元素是否相等即可。
#### 动画描述
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190525181152.gif
)
#### 代码实现
```
java
///简单粗暴,看看就行
class
Solution
{
public
boolean
isPalindrome
(
int
x
)
{
String
reversedStr
=
(
new
StringBuilder
(
x
+
""
)).
reverse
().
toString
();
return
(
x
+
""
).
equals
(
reversedStr
);
}
}
```
### 解法二:进阶解法---数学解法
通过取整和取余操作获取整数中对应的数字进行比较。
举个例子:1221 这个数字。
-
通过计算 1221 / 1000, 得首位1
-
通过计算 1221 % 10, 可得末位 1
-
进行比较
-
再将 22 取出来继续比较
#### 动画描述
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190525181202.gif
)
#### 代码实现
```
java
class
Solution
{
public
boolean
isPalindrome
(
int
x
)
{
//边界判断
if
(
x
<
0
)
return
false
;
int
div
=
1
;
//
while
(
x
/
div
>=
10
)
div
*=
10
;
while
(
x
>
0
)
{
int
left
=
x
/
div
;
int
right
=
x
%
10
;
if
(
left
!=
right
)
return
false
;
x
=
(
x
%
div
)
/
10
;
div
/=
100
;
}
return
true
;
}
}
```
### 解法三:进阶解法---巧妙解法
直观上来看待回文数的话,就感觉像是将数字进行对折后看能否一一对应。
所以这个解法的操作就是
**取出后半段数字进行翻转**
。
这里需要注意的一个点就是由于回文数的位数可奇可偶,所以当它的长度是偶数时,它对折过来应该是相等的;当它的长度是奇数时,那么它对折过来后,有一个的长度需要去掉一位数(除以 10 并取整)。
具体做法如下:
-
每次进行取余操作 ( %10),取出最低的数字:
`y = x % 10`
-
将最低的数字加到取出数的末尾:
`revertNum = revertNum * 10 + y`
-
每取一个最低位数字,x 都要自除以 10
-
判断
`x`
是不是小于
`revertNum`
,当它小于的时候,说明数字已经对半或者过半了
-
最后,判断奇偶数情况:如果是偶数的话,revertNum 和 x 相等;如果是奇数的话,最中间的数字就在revertNum 的最低位上,将它除以 10 以后应该和 x 相等。
#### 动画描述
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190525181211.png
)
#### 代码实现
```
java
class
Solution
{
public
boolean
isPalindrome
(
int
x
)
{
//思考:这里大家可以思考一下,为什么末尾为 0 就可以直接返回 false
if
(
x
<
0
||
(
x
%
10
==
0
&&
x
!=
0
))
return
false
;
int
revertedNumber
=
0
;
while
(
x
>
revertedNumber
)
{
revertedNumber
=
revertedNumber
*
10
+
x
%
10
;
x
/=
10
;
}
return
x
==
revertedNumber
||
x
==
revertedNumber
/
10
;
}
}
```
![](
https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录