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 ...
随机推荐
- HDU - 1174:爆头 (三维平面点到射线的距离)
pro:给定警察的射击位置,设计方向,敌人的位置,敌人的头部半径,问子弹是否可以射到头部. sol:即问头部中点到子弹射线的距离是否小于等于头部半径. 和二维的点到直线一样的操作. det/dot: ...
- webpack的一些详细配置
http://blog.csdn.net/c_kite/article/details/71279853
- Apache Kafka 源码剖析
Getting Start 下载 http://kafka.apache.org/ 优点和应用场景 Kafka消息驱动,符合发布-订阅模式,优点和应用范围都共通 发布-订阅模式优点 解耦合 : 两个应 ...
- 《DSP using MATLAB》Problem 5.12
1.从别的地方找的证明过程: 2.代码 function x2 = circfold(x1, N) %% Circular folding using DFT %% ----------------- ...
- java知识整理
整理一下Java知识点. 一.final finally finalize区别 1.final 修饰符(关键字).被final修饰的类,不能再派生出新的子类,不能作为父类而被子类继承.因此一个类不能既 ...
- String、StringBuffer、StringBuidler 知识整理
String.StringBuffer.StringBuidler.这三个家伙,大家都不陌生,肯定也都会用.三者异同大家都能说出来,但是其根本原因是什么呢?带着下面问题,学习一下. 第一.String ...
- LeetCode - Find K Closest Elements
Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...
- Gravitational Teleport docker-compose简单运行
Gravitational Teleport 可以作为堡垒机进行使用,为了测试方便使用docker-compose 运行一个all in one 的demo 备注: 官方提供的docker-compo ...
- C libraries in Linux
Copy from a book. There are several C libraries to choose from. The main options are as follows: gli ...
- MySQL插入,更新,删除数据
插入 单行插入 1.insert into 表名 values(col1_value,col2_value,...); 每个列必须提供一个值,如果没有值,要提供NULL值 每个列必须与它在表中定义的次 ...