这道题是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. Android使用MediaRecorder和Camera实现视频录制及播放功能整理

    转载请注明出处:http://blog.csdn.net/woshizisezise/article/details/51878566 这两天产品经理向我丢来一个新需求,需要在项目里添加一个视频录制的 ...

  2. Gitlab User Guide

    Installation Configuration Set user name and email Add SSH keys Repository Create New Repository Clo ...

  3. 遍历PspCidTable枚举进程

    //测试环境:win7 32位 1 // DriverEntry.cpp #include "ntddk.h" #include <ntddvol.h> #includ ...

  4. Java数据结构和算法(五)--希尔排序和快速排序

    在前面复习了三个简单排序Java数据结构和算法(三)--三大排序--冒泡.选择.插入排序,属于算法的基础,但是效率是偏低的,所以现在 学习高级排序 插入排序存在的问题: 插入排序在逻辑把数据分为两部分 ...

  5. java 一个对象多少大,占用多少内存

    1.instrumentation这种方法还是靠谱的 一个对象占用多少字节? 2.sizeof库 <!-- https://mvnrepository.com/artifact/com.carr ...

  6. oracle row_number的使用

    create table studentInfo(  id number(8) primary key,  name varchar2(20) not null,  ObjectName varcha ...

  7. SVN中检出(check out) 跟导出(export) 的区别

    SVN中检出(check out) 和导出(export) 的区别?观点一:SVN是常用的一种常见的版本控制软件.SVN中检出(check   SVN中检出(check out) 和导出(export ...

  8. js基本语法之 值类型(数据类型)(变量类型)

    一.不可改变的原始值(栈数据)(五个) 数字(number),字符串(string),布尔值(boolean),undefined,null 其中;undefined是未定义的意思,而null是空的意 ...

  9. mysqldump 备份导出数据排除某张表或多张表

    可以使用--ignore-table=dbname.tablename 忽略一张表 /usr/bin/mysqldump --set-gtid-purged=OFF -h127.0.0.1 -uroo ...

  10. Linux基础学习-MariaDB数据库管理系统

    数据库管理系统 数据库是指按照某些特定结构来存储数据资料的数据仓库,数据库管理系统是一种能够对数据库中存放的数据进行建立.修改.删除.查找.维护等操作的软件程序. 初始化MariaDB服务 [root ...