# LeetCode集锦(三) - 第9题 Palindrome Number

栏目: 编程工具 · 发布时间: 4年前

内容简介:确定整数是否是回文。当一个整数向后读取与向前读取相同的内容时,它就是一个回文。示例1:输入:121 输出:真正的
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. 

 Example 1: 


Input: 121
Output: true


 Example 2: 


Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.


 Example 3: 


Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.


 Follow up: 

 Coud you solve it without converting the integer to a string? 
复制代码

翻译:

确定整数是否是回文。当一个整数向后读取与向前读取相同的内容时,它就是一个回文。

示例1:

输入:121 输出:真正的

示例2:

输入:-121 输出:假 说明:从左到右,是-121。从右到左,变成121-。因此它不是回文。

示例3:

输入:10 输出:假 说明:从右向左读取01。因此它不是回文。

跟进:

你能在不把整数转换成字符串的情况下解出它吗?

解题思路

本题目标是判断一个数字是不是回文数字。

回文数字的定义:翻转数字后,和本身一样则为回文数字。

根据回文数字的定义,负数是不符合要求的,毕竟符号的限制存在,个位数一定是回文数字。

在定义中,是需要翻转数字,判断前后是否一致,很容易想到可以转化为String,然后翻转一下,是否一致就可以。

还有一种方式,就是一位位的取,重新组装数字,判断是否一致,但是这样需要避免溢出的问题。


以上所述就是小编给大家介绍的《# LeetCode集锦(三) - 第9题 Palindrome Number》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

JavaScript and Ajax for the Web, Sixth Edition

JavaScript and Ajax for the Web, Sixth Edition

Tom Negrino、Dori Smith / Peachpit Press / August 28, 2006 / $24.99

Book Description Need to learn JavaScript fast? This best-selling reference’s visual format and step-by-step, task-based instructions will have you up and running with JavaScript in no time. In thi......一起来看看 《JavaScript and Ajax for the Web, Sixth Edition》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具