leetcode:9 Palindrome Number-每日编程第二十八题
2015-12-15 21:45
267 查看
Palindrome Number
Total
Accepted: 95160 Total
Submissions: 317694 Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
思路:
1).使用while(x/10>base),而不是while(x>base){ base*=10}; base/=10; 是为了防止base溢出。
2).因为不允许使用额外空间,不能简单的将其转换为数组形式,对比首尾是否相等,所以,就只能轮流求出首尾数字值,比较其是否相等,不相等,则返回false。
Total
Accepted: 95160 Total
Submissions: 317694 Difficulty: Easy
Determine whether an integer is a palindrome. Do this without extra space.
思路:
1).使用while(x/10>base),而不是while(x>base){ base*=10}; base/=10; 是为了防止base溢出。
2).因为不允许使用额外空间,不能简单的将其转换为数组形式,对比首尾是否相等,所以,就只能轮流求出首尾数字值,比较其是否相等,不相等,则返回false。
class Solution { public: bool isPalindrome(int x) { if(x<0){ return false; } int base = 1; while(x/10>=base){ base*=10; } int left,right; while(base>=10){ left=x/base; x-=left*base; right=x%10; x/=10; if(left!=right){ return false; } base/=100; } return true; } };
相关文章推荐
- Java&Xml教程(六)使用JDOM解析XML文件
- Spring IOC 和 AOP
- 进制转换
- java开发俄罗斯方块学习笔记 Day-4 显示
- 《利用python进行数据分析》读书笔记--第九章 数据聚合与分组运算(二)
- Python-MySQL summary
- 关于python的编解码(decode, encode)
- java.lang.RuntimeException
- C++函数运算符重载
- 在 Windows 上快速安装并运行 Laravel 5.x
- Java NIO使用及原理分析(二)
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- Java NIO使用及原理分析 (一)
- 黑马程序员--C语言自学笔记---04分支结构
- JAVA初学者(一)
- java流程
- Java--常用jar包用途
- C#学习笔记——FileStream读取文件示例
- java如何读取.properties文件下的信息
- Java中Properties类的操作