【leetcode】9. Palindrome Number
2016-05-17 14:25
99 查看
/** * Determine whether an integer is a palindrome. Do this without extra space. */ #include <iostream> #include <string> #include <vector> #include <math.h> #include <limits.h> using namespace std; #define NUMBER(x, n) ((n) == 1 ? (x)%10 : (x) / (int)pow(10, (n-1)) % 10) bool isPalindrome(int x) { if (x < 0 || x > INT_MAX) { return false; } int len = 0; int tmp = x; while (tmp != 0) { tmp = tmp / 10; len++; } int high = 0, low = 0; int h, l; for (int i = 1; i <= len; i++) { low = i; high = len - i + 1; l = NUMBER(x, high); h = NUMBER(x, low); if (l != h) return false; } return true; } //leetcode比较优秀的答案 bool isPalindrome(int x) { if (x<0 || (x != 0 && x % 10 == 0)) return false; int sum = 0; while (x>sum) { sum = sum * 10 + x % 10; x = x / 10; } return (x == sum) || (x == sum / 10); } int main() { int num = 3445543; cout << isPalindrome(num); system("pause"); return 0; }
相关文章推荐
- 代码设置setLayoutParams(lp);总崩溃
- Retina屏实现1px边框
- javafx 同时画line chart和bar chart
- [git]安装git-pylint-commit-hook提高python项目中的代码质量
- jsp基本语法
- ResourceBundle.getBundle("baseName")的使用
- 隐马尔可夫模型(HMM) - 2 - 概率计算方法
- 架构漫谈(二):认识概念是理解架构的基础
- 【Sass-03】Sass官网文档
- android保存图片到本地并可以在相册中显示出来
- Linux top 命令
- 数据库优化
- Combination Sum II
- 自定义viewgroup实现ArcMenu
- NYOJ 1233 差值
- Java获取xml格式字段内容
- Java IO概述
- RxJava入门指引,易懂
- HDU 5680 zxa and set
- jqgrid快速入门之三:单元格输入文字的获取