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.

解法一:

  1. class Solution {
  2. public:
  3. /*
  4. * @param num: a positive number
  5. * @return: true if it's a palindrome or false
  6. */
  7. bool isPalindrome(int num) {
  8. //negative number
  9. if(num < )
  10. return false;
  11.  
  12. int len = ;
  13. while(num / len >= )
  14. len *= ;
  15.  
  16. while(num > ) {
  17.  
  18. //get the head and tail number
  19. int left = num / len;
  20. int right = num % ;
  21.  
  22. if(left != right)
  23. return false;
  24. else {
  25. //remove the head and tail number
  26. num = (num % len) / ;
  27. len /= ;
  28. }
  29. }
  30.  
  31. return true;
  32. }
  33. };

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

解法二:

  1. class Solution {
  2. public:
  3. /*
  4. * @param num: a positive number
  5. * @return: true if it's a palindrome or false
  6. */
  7. bool isPalindrome(int num) {
  8. if (num < )
  9. return false;
  10.  
  11. if (num < )
  12. return true;
  13.  
  14. int digits = ;
  15. int t = num;
  16. int d = ;
  17. while(t != ) t /= , ++d;
  18.  
  19. int left = pow(, d - );
  20. int right = ;
  21. while( left >= right)
  22. {
  23. if (num / left % != num / right % )
  24. return false;
  25.  
  26. left /= ;
  27. right *= ;
  28. }
  29. return true;
  30. }
  31. };

依旧是过滤出头尾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. 关于muse-ui 图片不显示的办法。

    直接使用框架提供的例子不能显示图片.需要npm安装 才可以直接使用. npm install --save material-design-icons

  2. Django 工作流程

    一.Django 工作流程 在开始具体的代码之旅前,先来宏观地看下Django是如何处理Http Resquest的,如下图: 假设你已经在浏览器输入了 http://127.0.0.1:8000/p ...

  3. 在做了 BasePage 时: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 / / 节中包括

    摘自: http://lichengguizy.blog.163.com/blog/static/11771858620122342749552/ 只有在配置文件或 Page 指令中将 enableS ...

  4. 一起talk GDB吧(第五回:GDB查看信息)

    各位看官们.大家好,上一回中我们说的是GDB的调用栈调试功能,而且说了怎样使用GDB进行查看调用 栈.这一回中,我们继续介绍GDB的调试功能:查看信息.当然了.我们也会介绍怎样使用GDB查看程序 执行 ...

  5. ZOJ3622 Magic Number(水题)

    分析: 举个样例xxx(三位数)为魔力数,则xxx|(xxx+1000*y),那么xxx|1000,这个就是结论 同理:四位数xxxx|10000,五位数xxxxx|100000 代码: #inclu ...

  6. Elastic修改副本数量

    分片的个数在创建之后是无法再增加和减少的,除非你另外建一个索引库,而副本是可以在运行的时候,动态增加和减少.因此,在创建索引库时,规划好分片(Shard)是非常重要的,而在插入大量数据时可以先将副本书 ...

  7. C语言面试问题

    内容源自:C语言面试题大汇总 P.S.只摘取了自己觉得可能会被问到的以及不会的. static有什么用途?(请至少说明两种) 1.限制变量的作用域2.设置变量的存储域 引用与指针有什么区别? 1) 引 ...

  8. docker学习笔记二:常用命令

    docker学习笔记二:常用命令 查看docker常用命令 docker --help 返回结果如下: 其中常用的命令如下: 1.image相关操作 展示所有的image: 删除image: rmi ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何声明定时器,使用定时器TON模块 TC3

    TON功能块功能: 当输入为高电平时,计时器开始计时,CV表示计时器计时的当前值,而PV则是计时的目标值,当CV的值等于PV的值时,输出置1.     在主程序接下去的地方按下F2并添加TON功能块. ...

  10. v - bind

    1. 用于处理html标签的动态属性,即动态赋值(动态地绑定一个或多个特性,或一个组件 prop 到表达式) 2. 官网API <!DOCTYPE html> <html lang= ...