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 ...
随机推荐
- textarea(多行文本域)
多行文本域<textarea>: <textarea name="..." rows="..." cols="..." . ...
- MyBatis #{} 取值注意事项
正确写法#{key} 错误写法#{key } #{}中不能加空格,不然会报错
- css实现三栏布局,两边定宽,中间自适应
1.利用定位实现 css代码如下: .box{overflow: hidden;height: 100px;margin: 10px 0;} .box>div{height: 100%;} #b ...
- c++输入输出流加速器
发现同样是cin,cout,其他大佬(orz)的耗时短很多.看了他们的代码,我发现他们加了一个很神奇的匿名函数(Lambda捕获)提高了cin,cout效率,因此去百度了解了一下.以下是大佬所使用 ...
- 2018.4.17 VFS
总结: VFS只存在于内存中,它在系统启动时被创建,系统关闭时注销. VFS的作用就是屏蔽各类文件系统的差异,给用户.应用程序.甚至Linux其他管理模块提供统一的接口集合. 管理VFS数据结构的组成 ...
- test-ipv6
http://test-ipv6.com/ ! 你的公网 IPv4 地址是 89.42.31.211! 你的公网 IPv6 地址是 2001:ac8:21:8::376e:989b! 你已接入 IPv ...
- 在Java中,以下关于方法重载和方法重写描述正确的是?
public class TTTTT extends SuperC{ public String get(){ return null; } } class SuperC{ Object get(){ ...
- WebService 学习记录
-------------------------------------------PS:这个WebService 服务必须一直开着,关闭就没法访问了 Web Service 教程 一.webser ...
- pread和pwrite函数
先来介绍pread函数 [root@bogon mycode]# cat test.c #include<stdio.h> #include<stdlib.h> #includ ...
- 使用terraform-provider-s3 操作minio
尽管默认官方提供了s3 的操作,但是对于开源minio 无法支持,更多的是aws 的s3,社区提供了一个通用 s3 操作的provider(基于minio 的sdk) 环境准备 docker-comp ...