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基础入门教程(四)

    说明 前面三篇博客介绍了js中基本的知识点,包括变量类型及其转换.表达式.运算符等小知识点,这篇博客主要讲的是对象.如果你学过java等语言,你也许在下文的阅读中发现在js中的对象与java中的对象存 ...

  2. 【mybatis】mybatis多表联查,存在一对多关系的,实体中使用List作为字段接收查询结果的写法

    实体如下: IntegralGoods  积分商品 IntegralGoodsImg 积分商品图片 ShelfLog 积分商品自动上架记录 IntegralGoods :IntegralGoodsIm ...

  3. 【RocketMQ】【分布式事务】使用RocketMQ实现分布式事务

    参考地址:https://blog.csdn.net/zyw23zyw23/article/details/79070044 视频地址:https://v.youku.com/v_show/id_XO ...

  4. iOS Web应用开发:运用HTML5、CSS3与JavaScript

    <iOS Web应用开发:运用HTML5.CSS3与JavaScript> 基本信息 原书名:Pro iOS web design and development:HTML5, CSS3, ...

  5. 19个三维GIS软件对比

    19个三维GIS软件对比 麦豆科研技术中心 days ago 我国GIS经过三十多年的发展,理论和技术日趋成熟,在传统二维GIS已不能满足应用需求的情况下,三维GIS应运而生,并成为GIS的重要发展方 ...

  6. 如何在Centos7上安装zookeeper 多实例

    一.如何在Centos7上安装zookeeper 多实例 cd /usr/local/src/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zoo ...

  7. Tile based Rendering //后面一段是手机优化建议

    https://www.imgtec.com/blog/a-look-at-the-powervr-graphics-architecture-tile-based-rendering/ 一种硬件结构 ...

  8. PHP中 "{}" 大括号的用法和总结

    在PHP中,大括号“{}”可以起到如下作用: 1.将多个独立语句合并为一个复合语句,例如 if ... else ...中经常如此使用 2.在变量间接引用中进行定界,避免歧义.例如 ${$my_var ...

  9. [ES6] 10. Array Comprehensions

    ES6 decided that Array Comprehensions will not included in this version, ES7 will include this. Ther ...

  10. 利用HTML5与jQuery技术创建一个简单的自动表单完成

    来源:GBin1.com 在线演示   在线下载 谷歌快速搜索自带大量自动完成插件——库中甚至还有一个附带的jQuery UI共享选项.然而今天我要寻找一个替代的解决方案.由DevBridge开发的j ...