Check a positive number is a palindrome or not.

A palindrome number is that if you reverse the whole number you will get exactly the same number.

Notice

It's guaranteed the input number is a 32-bit integer, but after reversion, the number may exceed the 32-bit integer.

 
Example

11, 121, 1, 12321 are palindrome numbers.

23, 32, 1232 are not palindrome numbers.

解法一:

 class Solution {
public:
/*
* @param num: a positive number
* @return: true if it's a palindrome or false
*/
bool isPalindrome(int num) {
//negative number
if(num < )
return false; int len = ;
while(num / len >= )
len *= ; while(num > ) { //get the head and tail number
int left = num / len;
int right = num % ; if(left != right)
return false;
else {
//remove the head and tail number
num = (num % len) / ;
len /= ;
}
} return true;
}
};

分别过滤出头尾2个数进行比较

解法二:

 class Solution {
public:
/*
* @param num: a positive number
* @return: true if it's a palindrome or false
*/
bool isPalindrome(int num) {
if (num < )
return false; if (num < )
return true; int digits = ;
int t = num;
int d = ;
while(t != ) t /= , ++d; int left = pow(, d - );
int right = ;
while( left >= right)
{
if (num / left % != num / right % )
return false; left /= ;
right *= ;
}
return true;
}
};

依旧是过滤出头尾2个数进行比较,处理的方式和解法一稍有不同,参考@MagiSu 的代码

491. Palindrome Number【easy】的更多相关文章

  1. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  2. 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  3. 82. Single Number【easy】

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it.   Example Given [1,2,2,1,3,4, ...

  4. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  5. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  6. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  7. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  8. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  9. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

随机推荐

  1. JavaScript基础入门教程(一)

    本系列教程的说明 本教程说白了可以说是我自己学习JavaScript的笔记,主要内容参考自<JavaScript权威指南>,部分内容可能来自互联网,本系列教程假设学者之前学过c或者其它的编 ...

  2. 【chrome】在做项目使用chrome调试的时候,调整Console的位置

    在新的电脑上安装了谷歌浏览器 ,然后在调试系统的时候,发现console这个控制台,模拟调试js的位置无法显示到source以下, 解决问题: 怎么样让console控制台显示到sources下,在查 ...

  3. C,C++开源项目中的100个Bugs

    俄罗斯OOO Program Verification Systems公司用自己的静态源码分析产品PVS-Studio对一些知名的C/C++开源项目,诸如Apache Http Server.Chro ...

  4. Go -- FileManage 自建云盘

    一.介绍 Caddy,用Go写的一款相当优秀的Web服务器软件,它有不少很有特色的功能,国内目前来说用的不多,不过也逐渐有越来越多的人知道了,它有个特色的插件功能,其中一款插件是FileManager ...

  5. kibana-sentinl-监控报警

    kibana 安装 sentin 插件 ./bin/kibana-plugin install https://github.com/sirensolutions/sentinl/releases/d ...

  6. CKFinder的_thumbs缓存文件夹

    <CKFinder2.0.2的使用与破解> 作者: 宓晨        说下背景,发下牢骚!        由于网站需要支持对图片的上传以及操作的管理,还有文字+图片的组合上传.问了一下同 ...

  7. 轻松编写 C++ 单元测试

    单元测试概述 测试并不只是测试工程师的责任,对于开发工程师,为了保证发布给测试环节的代码具有足够好的质量( Quality ),为所编写的功能代码编写适量的单元测试是十分必要的. 单元测试( Unit ...

  8. 【架构】使用OpenStack、AliYun、AWS、Docker打造融合的IAAS、PAAS平台

    GalaxyManager,即平台门户,旨在整合数据中心异构虚拟化资源为统一的资源池,并在资源池上为用户提供各类IAAS.PAAS服务. GitHub:https://github.com/junne ...

  9. App服务端架构变迁

    随着移动互联网时代的到来,移动技术也随之飞速发展.如今,App已然成为绝大多数互联网企业用来获取用户的核心渠道.以往以PC为主要承载平台的各业务线,源源不断集成加入到移动项目中来,原本以产品为中心快速 ...

  10. windows Server2012 之 IIS8.0配置安装完整教程

    IIS8.0是windows Server2012自带的服务器管理系统,和以往不同,IIS8.0安装和操作都比较简单,界面很简洁,安装也很迅速.今天我们重点完整的演示下Internet Informa ...