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 ...
随机推荐
- shell常用函数封装-main.sh
#!/bin/bash #sunlight sp monitor system #created on 2018/01/07#by chao.dong#used by sp servers consi ...
- 阿里云ECS服务器购买流程 (自定义配置购买、按月、按量购买)教程
阿里ECS云服务器自定义购买流程 本文提供全图文流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- java的InputStream和OutputStream的理解
注:参考链接:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616187.html 1.在java中stream代表一种数据流(源),jav ...
- error: ld returned 1 exit status 解决
1.程序未结束运行 2.全局变量冲突,不是宏定义冲突
- ubuntu软件管理
https://www.cnblogs.com/forward/archive/2012/01/10/2318483.html 一.Ubuntu中软件安装方法1.APT方式(联网安装, 需要联网下载软 ...
- [codeforces round#475 div2 ][C Alternating Sum ]
http://codeforces.com/contest/964/problem/C 题目大意:给出一个等比序列求和并且mod 1e9+9. 题目分析:等比数列的前n项和公式通过等公比错位相减法可以 ...
- 【BZOJ1213】高精度开根
python是坠吼的! 原题: 不贴原题,就是高精度开根,结果向下取整 首先二分答案,高精度嘛……python即可 二分右端点设为n会T掉,需要先倍增一个r,while(r **m <= n) ...
- redis源码之压缩列表ziplist
压缩列表ziplist1.简介连续,无序的数据结构.压缩列表是 Redis 为了节约内存而开发的, 由一系列特殊编码的连续内存块组成的顺序型(sequential)数据结构. 2.组成 属性 类型 长 ...
- Python-pycurl模块的安装
先执行以下命令(因为我在另一个终端执行,所以history的编号有重复) 7 wget https://pypi.python.org/packages/source/p/pycurl/pycurl- ...
- centos7.0之vsftpd随笔
yum install vsftpd -f安装vsftpd软件 systemctl start vsftpd 默认ftp目录为/var/ftp/,该文件夹下有pub文件夹 iptables -F 防火 ...