这道题是LeetCode里的第9道题。

题目说的:

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

示例 1:

输入: 121
输出: true

示例 2:

输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

示例 3:

输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。

进阶:

你能不将整数转为字符串来解决这个问题吗?

这道题虽然简单,但是对于简单题,我们要做到用比较骚的方法去解题,就像这道题一样,大部分人想到的是转字符串,然后双指针 blablabla......,不错,我也是这样想的。然而进阶的要求是不使用字符串,我首先想到的是求位数,其实就是变相的使用与字符串类似的方法,具体我就不介绍了,大家懂的都懂,这里我想要说的是用一种更巧妙的方法来解这道题。

我所做的:

class Solution {
public:
bool isPalindrome(int x) {
if(x<0||x>2147447412)return false;
int numSrc=x;
int numDst=0;
while(numSrc){
numDst=numDst*10+numSrc%10;
numSrc/=10;
}
return numDst==x;
}
};

我得到的:

太慢了,上几行代码加个速:

static int x = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();

我收获的:

我这道题之前的 if 条件是不包括 x>2147447412 的,后面官方加了个 2147483647 的实例,导致 int numDst 数据直接溢出,而且官方偷懒,可能只加了这一个实例,我加了这个条件后就通过了。我这个条件其实加了和没加一样,当 x=2147447399 时照样溢出,最好的解决办法是将 numDst 扩充为 long 型就一劳永逸了!

再上一个最快的解法:

static int x = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int reverseNumber = 0;
while (x > reverseNumber) {
reverseNumber = reverseNumber * 10 + x % 10;
x /= 10;
}
return x == reverseNumber || x == reverseNumber / 10;
}
};

这个方法是在我之上折半,最少缩短了一半的时间,而且还不用考虑数据溢出,节约内存,厉害!

static int x = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
bool isPalindrome(int x) {
string s;
ostringstream convert;
convert << x;
s = convert.str();
return equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
}
};

【LeetCode】Palindrome Number(回文数)的更多相关文章

  1. leetcode 9 Palindrome Number 回文数

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

  2. LeetCode Problem 9:Palindrome Number回文数

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

  3. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  4. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  5. [LeetCode]9. Palindrome Number回文数

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

  6. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  7. 【LeetCode】9 Palindrome Number 回文数判定

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

  8. 9. Palindrome Number 回文数的判断

    [抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...

  9. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

随机推荐

  1. body和普通div背景图宽高百分比的区别

    body和普通div背景图的区别  background: url(//m.360buyimg.com/mobilecms/s220x220_jfs/t2746/167/831241799/29915 ...

  2. 关于IE兼容的问题

    以下内容,均来自不同的网站,非本人原创,只是收集一下放在一起! =============================== [一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 ...

  3. ionic2 tabs 自定义图标

    ionic2 tabs 自定义图标 一.准备资源 tabs icon 的svg格式的矢量图片 二.生成字体样式文件 打开icoMoon网站去制作字体文件. 三.使用字体文件 解压下载的文件,将其中的f ...

  4. 【Web应用-Kudu】Kudu 管理和诊断 azure web 应用

    Azure  Kudu是 GitHub 上的一个开源项目,Kudu 站点 (也称为网站控制管理 SCM) 提供了一系列的在线工具,可以帮助用户查看 web 应用的设置,诊断 web 应用,以及安装 w ...

  5. LR中webservice服务测试的脚本

    Action(){ /* 测试QQ是否在线的功能接口 输入参数:QQ号码 String,默认QQ号码:8698053. 返回数据:String,Y = 在线:N = 离线:E = QQ号码错误:A = ...

  6. 洛谷 P1774 最接近神的人_NOI导刊2010提高(02)

    题目描述 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上方用古代文写着“神的殿堂”.小FF猜想里面应该就有王室的 ...

  7. 洛谷 P1901 发射站

    题目描述 某地有 N 个能量发射站排成一行,每个发射站 i 都有不相同的高度 Hi,并能向两边(当 然两端的只能向一边)同时发射能量值为 Vi 的能量,并且发出的能量只被两边最近的且比 它高的发射站接 ...

  8. ssh复制remote

    rsync rsync localdirectory username@10.211.55.4:/home/username/Downloads/localdirectory -r

  9. Maven添加本地依赖

    在写本文的时候先来说明一下maven依赖的各种范围的意思 compile(编译范围)       compile 是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范围.编译范围依赖在所有的c ...

  10. shell 简单脚本 2

    #!/bin/bash source /etc/profile APPLICATIONS_HOME="/cpic/cpicapp/cpic_analy/jars" APPLICAT ...