一.题目链接: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的更多相关文章

  1. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  4. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  5. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  6. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  7. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  8. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  9. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  10. 【leetcode】Palindrome Number

    题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

随机推荐

  1. pytorch实现style transfer

    说是实现,其实并不是我自己实现的 亮出代码:https://github.com/yunjey/pytorch-tutorial/tree/master/tutorials/03-advanced/n ...

  2. windows下前端开发工具遇到的问题总结(yeoman bower grunt)

    我用的是windows环境 一毕要环境: 1:nodejs 官网:https://nodejs.org/en/ 2:由于很多国外网站国内都访问不了(如果没有设置会出现很多奇怪的错误),所有必需FQ 我 ...

  3. thrift使用案例

    参考资料:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ 首先是定义thrift IDL接口,如下(SunTelTc.thri ...

  4. 20155219&20155224 《信息安全系统设计基础》实验一 开发环境的熟悉

    实验内容 实验两人一组,最多三人一组 可以使用自己的笔记本,也可以使用实验室台式机 使用实验箱作为超级终端. 实验中学到的知识点 交叉编译 宿主机与目标机 NFS 超级终端 file命令 实验步骤 实 ...

  5. hdu-2196 树形dp 求一个树中所有节点能到达的最远距离f[i] (其实也不难嘛!)

    #include <bits/stdc++.h> using namespace std; ; struct T { int to; int w; }; vector < vecto ...

  6. ccf-170904-通信网络

    ccf-170904-通信网络 题目分析: 有向图 如果a可以直接或者间接连接b则a与b相互知晓 一共有多少个点知道n个点 刚开始算错复杂度,优化后反而超时 ...事实无脑爆搜这道题也是可取的因为只有 ...

  7. unet网络讲解,附代码

    转: http://www.cnblogs.com/gujianhan/p/6030639.html key1: FCN对图像进行像素级的分类,从而解决了语义级别的图像分割(semantic segm ...

  8. 【HDOJ3567】【预处理bfs+映射+康拓展开hash】

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)    Me ...

  9. window.location.herf=url参数有中文,到后台乱码问题解决

    js中的代码: /*将中文的参数进行两次编码 */ function queryByName(){                    //获取查询条件的用户名                   ...

  10. Docker快速搭建PHP+Nginx+Mysql环境(https://notemi.cn/docker-quickly-set-up-php-nginx-mysql-environment.html)

    目录 1 · 目标 2 · 安装Docker 3 · 创建资源文件夹 4 · 部署Mysql · 1. 拉取镜像 · 2. 运行容器 · 3. 进入容器 · 4. 开启Mysql远程连接 5 · 部署 ...