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 ...
随机推荐
- 关于muse-ui 图片不显示的办法。
直接使用框架提供的例子不能显示图片.需要npm安装 才可以直接使用. npm install --save material-design-icons
- Django 工作流程
一.Django 工作流程 在开始具体的代码之旅前,先来宏观地看下Django是如何处理Http Resquest的,如下图: 假设你已经在浏览器输入了 http://127.0.0.1:8000/p ...
- 在做了 BasePage 时: 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 / / 节中包括
摘自: http://lichengguizy.blog.163.com/blog/static/11771858620122342749552/ 只有在配置文件或 Page 指令中将 enableS ...
- 一起talk GDB吧(第五回:GDB查看信息)
各位看官们.大家好,上一回中我们说的是GDB的调用栈调试功能,而且说了怎样使用GDB进行查看调用 栈.这一回中,我们继续介绍GDB的调试功能:查看信息.当然了.我们也会介绍怎样使用GDB查看程序 执行 ...
- ZOJ3622 Magic Number(水题)
分析: 举个样例xxx(三位数)为魔力数,则xxx|(xxx+1000*y),那么xxx|1000,这个就是结论 同理:四位数xxxx|10000,五位数xxxxx|100000 代码: #inclu ...
- Elastic修改副本数量
分片的个数在创建之后是无法再增加和减少的,除非你另外建一个索引库,而副本是可以在运行的时候,动态增加和减少.因此,在创建索引库时,规划好分片(Shard)是非常重要的,而在插入大量数据时可以先将副本书 ...
- C语言面试问题
内容源自:C语言面试题大汇总 P.S.只摘取了自己觉得可能会被问到的以及不会的. static有什么用途?(请至少说明两种) 1.限制变量的作用域2.设置变量的存储域 引用与指针有什么区别? 1) 引 ...
- docker学习笔记二:常用命令
docker学习笔记二:常用命令 查看docker常用命令 docker --help 返回结果如下: 其中常用的命令如下: 1.image相关操作 展示所有的image: 删除image: rmi ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何声明定时器,使用定时器TON模块 TC3
TON功能块功能: 当输入为高电平时,计时器开始计时,CV表示计时器计时的当前值,而PV则是计时的目标值,当CV的值等于PV的值时,输出置1. 在主程序接下去的地方按下F2并添加TON功能块. ...
- v - bind
1. 用于处理html标签的动态属性,即动态赋值(动态地绑定一个或多个特性,或一个组件 prop 到表达式) 2. 官网API <!DOCTYPE html> <html lang= ...