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. 第六章在U盘上运行openwrt(引导)--补

    1.前言 前面已经把U盘挂在了703N上了,现在只需要打开路由器,使用TTL串口或者putty(ssh模式需要用户名和密码-第一章刷openwrt的时候已经设置好)登陆路由器. 2.将系统内所有文件同 ...

  2. 关于使用Android新版Camera即Camera2的使用介绍 暨解决Camera.PreviewCallback和MediaRecorder无法同时进行

    新的相机API也就是Camera2是在Android 5.0引进的.通常情况下,我们都是使用Android旧的相机API,纵然在Android Studio里老是提示已经废弃,但是只要都能用,也就没必 ...

  3. mysql-operator 尝试与研究

    安装指南 先下载到本地 git clone https://github.com/kubernetes/charts.git 安装helm 参考: http://www.cnblogs.com/eri ...

  4. [转]SSIS ProtectionLevel 对包中敏感数据的访问控制

    本文转自:http://technet.microsoft.com/zh-cn/library/ms141747.aspx 为了保护 Integration Services 包中的数据,可以设置保护 ...

  5. go语言基础之基础数据类型 bool类型 浮点型

    1.bool类型 示例1: package main import "fmt" func main() { var a bool a = true fmt.Println(&quo ...

  6. TreeView 类 事件

    名称 说明 AfterCheck 在选中树节点复选框后发生. AfterCollapse 在折叠树节点后发生. AfterExpand 在展开树节点后发生. AfterLabelEdit 在编辑树节点 ...

  7. JedisCluster操作redis集群demo

    package com.chenk; import java.util.HashMap; import java.util.HashSet; import java.util.List; import ...

  8. Solidworks如何等比例缩小放大模型

    比如初始化的模型,笔记本长度只有120mm,实际上应该是3倍左右   右击特征,勾选模具工具,然后可以发现多出来一个页面   点击比例缩放,选中要缩放的特征,设置比例,然后打钩   可以发现已经缩放到 ...

  9. Building An Effective Marketing Plan

    “New ideas are a dime a dozen,” observes Arthur R. Kydd, “and so are new products and new technologi ...

  10. 百度URL參数解析

    百度URL參数解析 在用Python爬取百度搜索的内容时,发现百度搜索的url非常的长.往往会跟一大段的參数,但事实上非常多參数都是没有必要的,如相同是搜索javakeyword,能够通过 http: ...