491. Palindrome Number【easy】
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.
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】的更多相关文章
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 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 ...
- 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, ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- 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 ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- 【JSP JSTL】<c:if>多个判断条件 + <c:foreach>满足条件跳出循环
有一个需求,将所拥有的权限存放于session中,现在JSP页面判断这些如果在所有权限中有某一个或者某几个,就显示相对应的页面内容 举一个例子,实现以上的逻辑,<c:if>多个判断条件 + ...
- jquery的表单验证方法,一个function能不能同时捕捉点击事件和按键事件?能不能再优化下,有代码。
// 该jquery扩展引自 http://www.ghostsf.com/tools/389.html 方法名是作者博客的命名 $.fn.ghostsf_serialize = function ( ...
- latex 三个不同的图放在一行且每个图都有注释
\begin{figure}[htbp] \begin{minipage}[t]{0.3\linewidth} \centering \includegraphics[width=.2.0.eps} ...
- XSD-学习总结
1.代码详细分析 <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/X ...
- Node.js aitaotu图片批量下载Node.js爬虫1.00版
即使是https网页,解析的方式也不是一致的,需要多试试. 代码: //====================================================== // aitaot ...
- kubernetes 部署SonarQube 7.1 关联LDAP
之前有写过一篇如何在kubernetes上部署SonarQube的文档, 然后由于客户的需求,需要SonarQube关联LDAP的用户, 于是今天花了半天时间研究了以下如何在原有的基础上安装LDAP插 ...
- Unity3D入门基础之游戏对象 (GameObject) 和组件 (Component) 的关系
原文出处:http://edu.china.unity3d.com/learning_document/getData?file=/Manual/TheGameObject-ComponentRela ...
- recess----1.第一个APP-helloRecess
选择recess的理由很简单,这个架构看起来很轻量级,很简单.至少是写起应用来感觉不需要考虑太多和架构相关的东西.没有按作者给的过程一步步来,折腾了好久...在这里记录一下. 安装过程略,官网文档无压 ...
- UVA 10168 Summation of Four Primes(数论)
Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler p ...
- hadoop权威指南(第四版)要点翻译(4)——Chapter 3. The HDFS(1-4)
Filesystems that manage the storage across a network of machines are called distributed filesystems. ...