这道题是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. springBoot + mybatis实现执行多条sql语句出错解决方法

    在Idea中执行多条sql语句的修改(mybatis默认的是执行sql语句是执行单条,所以要执行多条的时候需要进行配置) 需要在连接字符串中添加上&allowMultiQueries=true ...

  2. android textview添加滚动条

    给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...

  3. 通过 Azure IoT 中心实现互联网设备数据的可视化分析

    本课程主要介绍了如何 在Azure 平台上借助 Azure IoT 中心, Azure 流分析,Web 应用, Azure 数据库等服务快速构建收集处理并可视化来自设备的数据流的应用, 包括项目背景介 ...

  4. COGS 1710. [POJ2406]字符串的幂

    ★☆   输入文件:powerstrings.in   输出文件:powerstrings.out   简单对比时间限制:3 s   内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...

  5. centos中安装elasticsearch5.0

    1.安装jdk 可以直接安装自带的openjdk,安装完成之后修改一下java的环境变量.另一种方式是就是安装oracle的jdk,从官网上下载http://www.oracle.com/techne ...

  6. 机器学习之-奇异值分解(SVD)原理详解及推导

    转载 http://blog.csdn.net/zhongkejingwang/article/details/43053513 在网上看到有很多文章介绍SVD的,讲的也都不错,但是感觉还是有需要补充 ...

  7. 量化投资,你需要了解的A股财务数据

    摘要:基本面量化是应用量化研究领域的重头戏,财务数据的整理和加工是基本面量化的第一步.本文梳理了财务数据的基本知识,包括报表类型.数据来源.调整更正和使用原则等,并给出了单季度和TTM数据的计算流程. ...

  8. fossil 代理设置

    C:\>fossil user new Joe C:\>fossil user default Joe 设置账户 fossil setting proxy http://-:-fossil ...

  9. 【转载】K-mer算法

    k-mer是指将reads分成包含k个碱基的字符串,一般长短为m的reads可以分成m-k+1个k-mers.举个例子吧,为了简化,有这么个reads(当然实际比这个长):AACTGACTGA.如果k ...

  10. Paxos算法与Zookeeper分析,zab (zk)raft协议(etcd) 8. 与Galera及MySQL Group replication的比较

    mit 分布式论文集 https://github.com/feixiao/Distributed-Systems wiki上描述的几种都明白了就出师了 raft 和 zab 是类似的,都是1.先选举 ...