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 ...
随机推荐
- openGl 基础
最近由于手机项目中需要用到OpenGL ES的知识,所以这段时间正在研究OpenGL的相关知识.因为OpenGL ES是OpenGL的剪裁版本,所以我直接从OpenGL入手,然后再去看OpenGL E ...
- 如何提高iOS开发能力
① 阅读技术博客 在现在这个年代,博客的风头早已被微博盖过.但是每天早上上班后的半小时,一定是打开博客,其中有讨论技术的,也有总结个人的相关经历,读完后肯定会有所收获.阅读博客,还有一个原因是技术博客 ...
- C,C++经典问题
C,C++经典问题 1 编程基础 1.1 基本概念 1.1.1 指针的理解:const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. ...
- [转]sql server transaction
本文转自: http://www.2cto.com/database/201208/146734.html sql事务(Transaction)用法介绍及回滚实例 事务(Transaction)是 ...
- IP地址网段规划
- Verilog语法
语法子集很小,易用. 模块:module…endmodule 端口:input,output,inout(双向特殊) inout比较难用,有一张真值表,需要大家观察后书写,基本原则就是输入时一定是高阻 ...
- Activex打包于发布完整版---微软证书制作
众所周知,Activex组件没有进行有效的签名,在IE上无法安装的,除非你让用户手工开启“接收任何未签名的ActiveX”,这个很明显不现实.而组件签名需要证书,证书从哪里来,你可以选择付1000到3 ...
- 不输入sudo使用docker
系统是debian系 安装: sudo apt install docker.io 将当前用户加入‘docker’组: sudo gpasswd -a ${USER} docker 刷新权限: su ...
- Android BaseAdapter和ViewHolder 优化 解决ListView的item抢焦点问题和item错乱问题
首先赞下hyman大神 曾经仅仅是简单的重写个BaseAdapter,将getView方法保持抽象.而ViewHolder没有抽象过. .. ViewHolder (用了一个集合+泛型管理存取view ...
- ES6 数值扩展
1.Number.isNan 和 Number.isFinite Number.isNaN()用来检查一个值是否为NaN Number.isNaN(NaN) // true Number.isNaN( ...