Leetcode:Palindrome Number
发布日期:2021-06-24 18:30:32 浏览次数:2 分类:技术文章

本文共 1138 字,大约阅读时间需要 3 分钟。

Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

注意:oj里面把负数视为非回文数

 

提示里面说可以把数字倒转过来,如果和原来数字相同那么就是回文数,但是数字倒转后可能越界,比如2147483647倒转后7463847412就超过了无符号整数的最大值  

 

只能逐个比较整数的第一个字符和最后一个字符,比如12321 第一次比较成功后数字变为232,再次比价后变为3    

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class 
Solution {
public
:
    
bool 
isPalindrome(
int 
x) {
        
if
(x < 0)
return 
false
;
        
int 
len = integerLength(x);
        
int 
factor1 =
pow
(10, len-1);
        
while
(x != 0)
        
{
            
if
(x / factor1 != x % 10)
                
return 
false
;
            
x %= factor1;
            
x /= 10;
            
factor1 /= 100;
        
}
        
return 
true
;
    
}
     
    
//求整数x的长度
    
int 
integerLength(
int 
x)
    
{
        
int 
res = 0;
        
while
(x)
        
{
            
x /= 10;
            
res++;
        
}
        
return 
res;
    
}
};
本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3676515.html,如需转载请自行联系原作者

转载地址:https://blog.csdn.net/weixin_34248258/article/details/90094873 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:如何调试DX程序
下一篇:排名 sql

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月26日 20时57分25秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

电子学会图形化scratch编程等级考试三级真题答案解析(选择题)2020-5 2019-04-28
【Scratch考级99图】图23-绘制复杂图形 2019-04-28
scratch森林的一天 电子学会图形化scratch编程等级考试一级真题编程题答案2020-9 2019-04-28
小兔子带你轻松玩转Scratch 绘制创意复杂图形24 2019-04-28
电子学会图形化scratch编程等级考试三级真题答案解析(判断题)2020-5 2019-04-28
小兔子带你轻松玩转Scratch 绘制创意复杂图形25 2019-04-28
电子学会图形化scratch编程等级考试四级真题答案解析(选择题)2020-6 2019-04-28
小兔子带你轻松玩转Scratch 绘制创意复杂图形26 2019-04-28
电子学会图形化scratch编程等级考试四级真题答案解析(判断题)2020-6 2019-04-28
Scratch克隆猫游戏 电子学会图形化编程scratch等级考试三级真题答案2020-5 2019-04-28
小兔子带你轻松玩转Scratch 绘制创意复杂图形27 2019-04-28
Scratch计算生肖 电子学会图形化编程Scratch等级考试四级真题答案解析2020-6 2019-04-28
Scratch数草莓 电子学会图形化编程Scratch等级考试四级真题答案解析2020-6 2019-04-28
电子学会图形化scratch编程等级考试二级真题答案解析(选择题)2020-9 2019-04-28
数据库课程设计 java实现学生信息管理系统 软件开发大作业 2019-04-28
电子学会图形化scratch编程等级考试四级真题答案解析(判断题)2020-12 2019-04-28
Scratch加密/解密 电子学会图形化编程Scratch等级考试四级真题 2020-12 2019-04-28
【蓝桥杯真题11】Scratch季节变化 少儿编程scratch蓝桥杯选拔赛真题讲解 2019-04-28
Scratch画雪花 电子学会图形化编程Scratch等级考试四级真题 2020-12 2019-04-28
初学python100例-案例29 判断回文数 少儿编程案例讲解 2019-04-28