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. JS的join方法

    join() 方法用于把数组中的所有元素放入一个字符串. 元素是通过指定的分隔符进行分隔的. 例子 1 在本例中,我们将创建一个数组,然后把它的所有元素放入一个字符串: <script type ...

  2. Ceph源码解析:CRUSH算法

    1.简介 随着大规模分布式存储系统(PB级的数据和成百上千台存储设备)的出现.这些系统必须平衡的分布数据和负载(提高资源利用率),最大化系统的性能,并要处理系统的扩展和硬件失效.ceph设计了CRUS ...

  3. 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。

    客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值.     无论是什么的html控件,只要加上了runat="server" ...

  4. 移动APP安全在渗透测试中的应用

    安全爱好者研究的往往是app的本地安全,比如远控.应用破解.信息窃取等等,大多人还没有关注到app服务端的安全问题,于是在这块的安全漏洞非常多. 移动app大多通过web api服务的方式跟服务端交互 ...

  5. DXR

    https://github.com/ConfettiFX/The-Forge/blob/master/CommonRaytracing_3/ThirdParty/DXR/doc/D3D12%20Ra ...

  6. C# 调用 Web Service 时出现 : 407 Proxy Authentication Required错误的解决办法

    // 记得 using System.Net; System.Net.WebProxy myProxy = new System.Net.WebProxy("localhost:9099&q ...

  7. 百度编辑器上传大视频报http请求错误怎么办

    百度编辑器UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码,所以受到很多开放人员的青睐.但是有时 ...

  8. jshint错误

    这条命令即可. npm install --save-dev jshint gulp-jshint

  9. 重载&lt;&lt;和&gt;&gt;

    在C++编程中实现数据的输入/输出能够用cin>>ch/cout<<ch; 可是使用cin>>ch或cout<<ch并不能实现一些特殊的数据的输入或者输 ...

  10. url 传值

    js获取url参数值: index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数 根据获取h ...