Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间)
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
分析:
回文数,并且题目要求不能使用额外的空间。
即,不能使用回文串的方法。
本题很简单,算出x的倒置数a,比较a是否和x相等就行了。
class Solution {
public:
bool isPalindrome(int x) {
//算出x的倒置数a,比较a是否和x相等就行了
int a = , b = x;
while(b > ){
a = a * + b % ;
b /= ;
}
if(a == x)
return true;
else
return false; }
};
网上其他的代码,其思路:每次提取头尾两个数,判断它们是否相等,判断后去掉头尾两个数
class Solution {
public:
bool isPalindrome(int x) { //negative number
if(x < )
return false; int len = ;
while(x / len >= )
len *= ; while(x > ) { //get the head and tail number
int left = x / len;
int right = x % ; if(left != right)
return false;
else {
//remove the head and tail number
x = (x % len) / ;
len /= ;
}
} return true;
}
};
Leetcode 9. Palindrome Number(判断回文数字)的更多相关文章
- [LeetCode]9. Palindrome Number判断回文数字
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) r ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode OJ Palindrome Number(回文数)
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+ ...
- LeetCode 9. Palindrome Number(回文数)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
随机推荐
- Hibernate入门(1)-第一个Hibernate程序
Hibernate入门(1)-第一个Hibernate程序 Hibernate是最著名的ORM工具之一,本系列文章主要学习Hibernate的用法,不涉及Hibernate的原理.本文介绍第一个Hib ...
- [iOS UI进阶 - 1] 自定义控件
A.关于Quiartz2D的一些细节 1.UIKit的工具已经封装了上下文引用,所以不用手动获取和渲染 - (void)drawRect:(CGRect)rect { [[UIColor redCol ...
- C# 中的sealed修饰符学习
转载原地址 http://developer.51cto.com/art/200908/147327.htm C#语言还是比较常见的东西,这里我们主要介绍C# sealed修饰符,包括介绍两个修饰符在 ...
- 使用friso中文分词注意
friso是使用c语言开发的一款中文分词器,使用流行的mmseg算法实现.完全基于模块化设计和实现,可以很方便的植入到其他程序中,例如:MySQL,PHP等.目前只支持UTF-8 编码. 源码无需修改 ...
- MS-SQL Server字符串处理函数大全
MS-SQL Server字符串处理函数大全 select语句中只能使用sql函数对字段进行操作(链接sql server), select 字段1 from 表1 where 字段1.Index ...
- 如何在Android中使用OpenCV
如何在Android中使用OpenCV 2011-09-21 10:22:35 标签:Android 移动开发 JNI OpenCV NDK 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始 ...
- scons用户指南翻译(附gcc/g++参数详解)
scons用户指南 翻译 http://blog.csdn.net/andyelvis/article/category/948141 官网文档 http://www.scons.org/docume ...
- Unity3D - 关于Dynamic和Static
含有Collider和RigidBody的GameObject, Unity视之为Dynamic 含有Collider的GameObject, Unity视之为Static 如果Static的物体发生 ...
- 教你50招提升ASP.NET性能(二十三):StringBuilder不适用于所有字符串连接的场景;String.Join可能是
(41)StringBuilder is NOT the answer for all string concatenation scenarios; String.Join could be 招数4 ...
- cocos2d-x CCArray
转自:http://blog.csdn.net/onerain88/article/details/8164210 1. CCArray只是提供了一个面向对象的封装类 其继承于CCObject类(CC ...