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 ...
随机推荐
- [C语言 - 10] C语言保留字
一 数据类型关键字 12 个: 1 . char 2 . short 3 . int 4 . long 5. enum 6. float 7. dou ...
- java tools: jmap
SYNOPSIS jmap [ option ] pid click here to see detail DESCRIPTION jmap prints shared object memory m ...
- SQLServer2008设置开启INTERNET远程连接
SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置: 使用sa账户登录SQL Se ...
- 谈谈 JavaScript 中的 this 指向问题
JavaScript 中的 this 为一个重难点,它不像静态语言 C#.Java 一样,就表示当前对象.而在 JS 中, this 是运行时确定,而并非定义时就已确定其值. 谈起 this ,必须少 ...
- mysql与java数据类型对照
类型名称 显示长度 数据库类型 JAVA类型 JDBC类型索引(int) 描述 VARCHAR L+N VARCHAR java.lang.String 12 CHAR N CHAR java.lan ...
- Oracle新建用户、角色,授权,建表空间
oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...
- 解决Linux下sqlplus中文乱码问题
错误现象:在windows下用其他工具访问oracle,确认中文正常显示.在Linux下使用sqlplus查询数据表中文内容出现乱码. 分析及解决:因为windows下正常,所以问题应出现在Linux ...
- cocos2d-x的初步学习二十一之iosandroid跨平台环境配置
这篇文章中,我们将来构建下跨平台开发的环境配置,我自己也是参考了别人了文章,折腾了几个小时,尤其是android的配置相对麻烦些.... 参考自子龙山人:http://www.cnblogs.com/ ...
- ThinkPad指纹验证在win7无法使用的解决方法
原先本本装window7 64bit 专业版(正版),但用着用着觉得 很不爽 ,反应特慢.所以决定对本本来次大换血,换成windows server 2008 R2.最后在装指纹验证的时候,使用超级管 ...
- 数据字符集mysql主从数据库,分库分表等笔记
文章结束给大家来个程序员笑话:[M] 1.mysql的目录:在rpm或者yum安装时:/var/lib/mysql 在编译安装时默许目录:/usr/local/mysql 2.用rpm包安装的MyS ...