Palindrome Number leetcode java
题目:
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.
题解:
最开始是用Reverse Integer的方法做的,通过了OJ,不过那个并没有考虑越界溢出问题。遇见这种处理Number和Integer的问题,首要考虑的就是会不会溢出越界。然后再考虑下负数,0。还有就是要心里熟悉\的结果和%的结果。感觉这种题就是考察你对数字敏感不敏感(反正我是很不敏感)
有缺陷的代码如下:
1 public boolean isPalindrome(int x) {
2 if(x<0)
3 return false;
4
5 int count = 0;
6 int testx = x;
7 while(testx!=0){
8 int r = testx%10;
9 testx = (testx-r)/10;
count++;
}
int newx = x;
int result = 0;
while(newx!=0){
int r = newx%10;
int carry = 1;
int times = count;
while(times>1){
carry = carry*10;
times--;
}
result = result+r*carry;
newx= (newx-r)/10;
count--;
}
return result==x;
}
另外一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。
代码如下:
1 public boolean isPalindrome(int x) {
2 //negative numbers are not palindrome
3 if (x < 0)
4 return false;
5
6 // initialize how many zeros
7 int div = 1;
8 while (x / div >= 10) {
9 div *= 10;
}
while (x != 0) {
int left = x / div;
int right = x % 10;
if (left != right)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
Reference:
http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/
Palindrome Number leetcode java的更多相关文章
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Palindrome Number ---- LeetCode 009
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- Ugly Number leetcode java
问题描述: Write a program to check whether a given number is an ugly number. Ugly numbers are positive n ...
- Single Number leetcode java
问题描述: Given an array of integers, every element appears twice except for one. Find that single one. ...
- Palindrome Partitioning leetcode java
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Valid Number leetcode java
题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
随机推荐
- QT学习笔记9:QTableWidget的用法总结
最近用QT中表格用的比较多,使用的是QTableWidget这个控件,总结一下QTableWidget的一些相关函数. 1.将表格变为禁止编辑: tableWidget->setEditTrig ...
- CF23 E. Tree 树形dp+高精度
题目链接 CF23 E. Tree 题解 CF竟让卡常QAQ dp+高精度 dp[x][j]表示以x为根的子树,x所属的联通块大小为j,的最大乘积(不带j这块 最后f[x]维护以x为根的子树的最大答案 ...
- Java内存泄露分析和解决方案及Windows自带查看工具
Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用自已的亲身经历 ...
- B+/-Tree原理
B-Tree介绍 B-Tree是一种多路搜索树(并不是二叉的): 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3. ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- ASP.NET Core 中的框架级依赖注入
https://tech.io/playgrounds/5040/framework-level-dependency-injection-with-asp-net-core 作者: Gunnar P ...
- HDU 4818 Golden Radio Base (2013长春现场赛B题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 进制转换. 现场根据题目给的两个公式,不断更新!!! 胡搞就可以了. 现场3A,我艹,一次循环开 ...
- Matlab 7.1安装及打不开问题解决
一.安装方法 1.解压[MATLAB.V7.1.Windows版本号].MATLAB.V7.1.R14.SP3.CD1.iso,双击setup进行安装,输入username,单位,找到crac ...
- Win10年度更新开发必备:VS2015 Update 2正式版下载汇总
========================================================================== 微软在03月30日发布了Visual Studio ...
- 蜻蜓FM涉嫌诈骗投资人和广告主源代码剖析
引用自:https://github.com/cryfish2015/QingTingCheat 本文主要内容,引用自知乎的这篇文章:如何评价蜻蜓 FM 伪造用户活跃度等数据 感谢“左莫”.“任正”等 ...