Palindrome Number
2015-08-16 16:14
344 查看
Determine whether an integer is a palindrome. Do this without extra space.
思路:
逆转该数字,看逆转前后是否相等。
确定该数的数量级,一次由两边想中间比较对称的两个数是否相等。
需要注意的是负数是palindrome。
思路:
逆转该数字,看逆转前后是否相等。
确定该数的数量级,一次由两边想中间比较对称的两个数是否相等。
需要注意的是负数是palindrome。
class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; else if(x >=0 && x < 10) return true; else{ long ret = 0, tmp = x; while(tmp){ ret = ret * 10 + tmp % 10; tmp = tmp / 10; } return int(ret) == x; } } };
class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; else if(x >=0 && x < 10) return true; else{ int len = 1;//数量级 while(x/len >= 10) len *= 10; while(x){ int r = x % 10; int l = x / len; if(r != l) return false; else{ x = (x % len) / 10; len /= 100; } } return true; } } };
相关文章推荐
- UVa 340 Master-Mind Hints
- Google笔试(2015年8月)
- 硬直
- BIOS中断大全
- 图像处理
- 2016年 成渝校招软件上机练习
- 彻底删除mysql的方法(有隐藏文件)
- EATool-输电线路房屋分布图
- 背包之01背包、完全背包、多重背包详解(转)
- c++操作符重载 类成员函数 类友员函数 左值 右值
- Jump Game II
- Ubuntu14.04_X64 + ATI显卡 安装Caffe
- TeXstudio 编写Latex论文的若干问题
- DC匹配中遇到的小问题
- 线程间的经典案例
- 装饰设计模式
- HDU 1009.FatMouse' Trade【贪心算法】【8月16】
- POJ 1273 Drainage Ditches 最大流
- Java基础 笔记(1)
- 回溯法之Fire Net