LeetCode——9. Palindrome Number
一.题目链接:https://leetcode.com/problems/palindrome-number/
二.题目大意:
给定一个整数,判断它是否为一个回文数。(例如-12,它就不是一个回文数;11它是一个回文数)
三.题解:
这道题目我一共用了两种解法:
方法1:将数字转化成字符串,然后首尾对应判断每个字符即可,代码如下:
class Solution {
public:
bool isPalindrome(int x) {
bool rs = true;
stringstream ss;
ss<<x;
string str = "";
str = ss.str();
int len = str.size();
int flag = 0;
for(int i = 0 ; i < len; i++)
{
if(str[i] == str[len -1 -i])
;
else
flag = 1;
}
if(flag == 1)
rs = false;
return rs;
}
};
这个是比较容易想到的方法,时间复杂度为O(n),空间复杂度为O(n)。
方法2:
该方法与第7题(7. Reverse Integer)的思想类似,从数字的末位开始相当于反转数字,反转的过程中如果反转的值和剩余的数字相同的话,那么它就是一个回文数字。代码如下:
class Solution {
public:
bool isPalindrome(int x) {
bool rs = true;
int sum = 0;
int temp = 0;
if(x < 0)
return false;
if(x >= 0 && x <=9)
return true;
if(x % 10 == 0)
return false;
while(x)
{
sum = sum *10+ x % 10;
x /= 10;
temp = x / 10;
if(temp && sum == temp)
return x;
if(sum == x)
return true;
}
return false;
}
};
该方法的时间复杂度为O(n),空间复杂度为O(1)。此方法有几处需要注意的点:
1.负数的话,一定不是回文数字。
2.该数字能够整除10的话,那么它也一定不是回文数字。(例如:1122110,如果直接执行while语句的部分的话,那么就会把它判断为回文数;而实际上它并不是回文数)
3.while语句中用了两个判断,其中sum == x是用于判断偶数位的情况(例如:1221);而sum == temp是用于判断奇数位情况的(例如:12321),此处一定注意两种情况都需要考虑!
4.对于x=0的情况,和x为一位数的情况,由于我while循环的条件是x不等于0并且循环体里面对单位数字无效;所以这两种情况单独讨论。
5.此处还有另一种方法,就是while循环体改造成如下的形式:
class Solution {
public:
bool isPalindrome(int x) {
bool rs = true;
int sum = 0;
int temp = 0;
if(x < 0 || (x && x % 10 == 0))//x不为0
return false;
if(x == 0)
return true;
while(x)
{
sum = sum *10+ x % 10;
if(sum == x)
return true;
x /= 10;
if(sum == x)
return true;
}
return false;
}
};
相当于x在整除10前后都与sum比较,在x整数10之前与sum比较也能解决奇数位数字的情况;但一开始的判断条件需要变一下。
LeetCode——9. Palindrome Number的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 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. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
随机推荐
- Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
Struts Problem Report Struts has detected an unhandled exception: Messages: Write operations are not ...
- GitHub教程手册、使用流程
简述GitHub的使用方法 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请调整网页缩放比例至200%) 1 注册过GitHub的朋友, ...
- ORB(oriented FAST and rotated BRIEF)特征提取与检测
ORB采取FAST算法检测特征点,采取BRIEF算法计算特征点描述子. 1.检测特征点 检测候选特征点周围一圈的像素值,若有足够多的像素值与候选特征点的差异都较大,则认为该候选特征点是特征点. 对于上 ...
- D:\yyy\UNetSegmentation_code_20180301\data\train
key1 numpy.save("filename.npy",a) 利用这种方法,保存文件的后缀名字一定会被置为.npy,这种格式最好只用 numpy.load("fil ...
- django -----分页器组件
分页器组件 本文目录 1 Django的分页器(paginator)简介 2 应用View层 3 模版层 index.html 4 扩展 回到目录 1 Django的分页器(paginator)简介 ...
- C++学习(二十一)(C语言部分)之 函数2
复习 函数名 函数名称 调用时需要函数名加上相应的参数函数类型 void 返回值(根据函数的需要)参数函数体 用来写函数的一个定义 函数怎样实现都现在其中函数声明 函数定义 定义如果放在调用的后面, ...
- tomcat localhost
启动tomcat后,登录本地localhost时,被要求输入用户名和密码,自己也从没有设置过啊,上网查找,原因如下: 机器装的oracle,它自带的httpserver的端口是8080,同时,tomc ...
- python------模块定义、导入、优化 ------->Yaml, l模块
一. yaml模块 用来做配置文件. 需要pip安装该包. 二. ConfigParser模块 用来生成和修改常见配置文件,在python3.x版本中更名为configparser. (什么是配置文件 ...
- springboot配置文件启动顺序
[1]项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 1.–f ...
- 给新创建的用户 赋予所有的权利 *.* 查看权限 删除用户 ---------DCL用户权限管理篇
第一步:进入数据库以后,先用 show databases; 再use mysql; 再 show tables; 再 select user,host from mysql.user; ...