9. Palindrome Number (JAVA)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
法I:逐一比较最左端与最右端的数字
class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int left; //the first bit of the inteter
int right; //the last bit of the integer
int divisor = 1; //divisor of each iteration
int tmp = x;
//determine the initial value of divisor
while(x/divisor >= 10){
divisor *= 10;
}
//check palindrome
while(x>0){
left = x/divisor; //divided by divisor to get the first bit
right = x%10; //mod 10 to get the last bit
if(left != right) return false;
x = (x%divisor)/10; //mode divisor to remove the first bit, divided by 10 to remove the last bit
divisor /= 100;
}
return true;
}
}
数字问题注意:
1. 负数
2. 变换后 以0开始 (本题中,如10101)这种情况,除以divisor = 0,mod 10 = 最后那位。
有多少个0,就得经过多少次循环,才能使得divisor的长度和被除数相等。在长度不等的时候,没次都循环末尾需为0,才能符合Palindrome的判断。所以该算法仍然可以验证有0开头的情况。
法II:逆向思维,如果是parlindrome,那么求出的反转数应等于x
这个方法的优点:只遍历了整数长度的一半。
class Solution {
public boolean isPalindrome(int x) {
if(x<0 //negative number
|| (x%10 == 0 && x != 0)) //the check"x == reverseNum/10" has one exception: reverseNum is one-bit number but x isn't, this case only exist in x%10 == 0 && x != 0
return false;
int reverseNum = 0;
while(x > reverseNum){
reverseNum = reverseNum * 10 + x%10;
x /= 10;
}
if(x == reverseNum || x == reverseNum/10) return true; //check parlindrome of number with odd or even length
else return false;
}
}
parlindrome问题注意:
1. 数字长度单、双分开讨论。
9. Palindrome Number (JAVA)的更多相关文章
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
- leetcode题解 9. Palindrome Number
9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...
随机推荐
- cordova网络情况检测插件使用:cordova-plugin-network-information
1. 添加插件 : cordova plugin add cordova-plugin-network-information 2. 调用方法: document.addEventListener(' ...
- 使用golang 编写postgresql 扩展
postgresql 的扩展可以帮助我们做好多强大的事情,支持的开发语言有lua.perl.java.js.c 社区有人开发了一个可以基于golang开发pg 扩展的项目,使用起来很方便,同时为我 ...
- 解决Visual Studio禁止使用strlen函数的问题
问题描述: 在学习C++的复制构造函数以及复制赋值运算符的重载时,需要用到使用C风格的字符串作为引入,由于我用的是VS2015(社区版),在编译时出错.编译器提醒strcpy函数是不安全的,建议改用s ...
- Python的深copy和浅copy
浅拷贝(copy):拷贝父对象,不会拷贝对象的内部的子对象. 深拷贝(deepcopy): copy 模块的 deepcopy 方法,完全拷贝了父对象及其子对象. 浅copy: a = [1, 2, ...
- 20165308 2017-2018-2 《Java程序设计》课程总结
20165308 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期待的师生关系 学习基础和c语言调查 Linux 安装及学习 第一周学习总结 第二 ...
- 初探JavaScript的截屏实现
最近参与了网易炉石盒子的相关页面开发,在做卡组分享页(地址:炉石盒子卡组分享),有个需求:用户可以把这个卡组以图片的形式分享给好友.最初的的做法是使用服务器把该页面转换成图片,然后把图片地址返回给前端 ...
- @Transactional 可以写在 Controller 方法上面了
上图 t1 掉用的service 没定义事物环境,但是 在 t1 上面定义了. 依旧可以 在 参数是5 的 时候 ,让 前面的操作级联回滚. 但是 我不建议这么用,除非特殊需求,正常来说事物根据 ...
- laravel whereNotIn where子查詢
子查詢寫法 $stores = Stores::select('id','name')->whereNotIn('id', function ($query) use($id){ $query- ...
- Numpy、SciPy、MatPlotLib在Python2.7.9下的安装与配置
前言: Python安装完Numpy,SciPy和MatplotLib后,可以成为非常犀利的科研利器.网上关于这三个库的安装都写得非常不错,但是大部分人遇到的问题并不是如何安装,而是安装好后因为配置不 ...
- Lepus监控之安装部署
PHP和Python都是跨平台的语言,所以理论上系统应该可以支持在不同的平台上运行.但是由于时间和精力以及资源有限,目前天兔系统只测试完善了Centos/RedHat系统的支持.我们目前提供的技术支持 ...