[LeetCode][Java] Palindrome Number
题目:
Determine whether an integer is a palindrome. Do this without extra space.
Some
hints:
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
》,将给定的整数转置。然后推断转置后的整数和原整数是否相等。假设相等就是回文数。这里注意一些特殊条件的推断。
AC代码:
public class Solution
{
private int i;
private int labelnum;
private long finalnum;
private int finalnnum;
private int checkresult;
private int label;
private int y;
private boolean blabel;
public boolean isPalindrome(int x)
{
y=reverse(x);//将原整数进行转置
if (y==0)
{
if(x==y) blabel=true;//转制前后都是0
else blabel=false;//原整数不是0,转置后变为0,表明出现溢出
}
else if(x<0)//负数不是回文数
blabel=false;
else if(x==y)
blabel=true;
else
blabel=false;
return blabel;
}
public int reverse(int x)
{
String s1=Integer.toString(x);
if(s1.charAt(0)=='-')
{
String s2="-";
String finals2="-";
String Judges2="";
long num1=0L;
for(i=s1.length()-1;i>=1;i--) s2+=s1.charAt(i);
for(i=1;i<s2.length();i++)
{
if(s2.charAt(i)!='0')
{
labelnum=i;
break;
} }
for(i=labelnum;i<s2.length();i++)
{
finals2+=s2.charAt(i);
}
label=checkstring(finals2);//检查是否存在溢出问题,label=1表明没有溢出;label=0表明产生溢出
if(label==1)
{
finalnum=Integer.parseInt(finals2);
}
else
{
finalnum=0;
} }
else
{
String s2="";
String finals2="";
String Judges2="";
long num1=0L;
for(i=s1.length()-1;i>=0;i--) s2+=s1.charAt(i);
for(i=0;i<s2.length();i++)
{
if(s2.charAt(i)!='0')
{
labelnum=i;
break;
}
}
for(i=labelnum;i<s2.length();i++)
{
finals2+=s2.charAt(i);
}
label=checkstring(finals2);//检查是否存在溢出问题。label=1表明没有溢出;label=0表明产生溢出
if(label==1)
{
finalnum=Integer.parseInt(finals2);
}
else{
finalnum=0;
} } return (int) finalnum;
} private int checkstring(String string)
{
checkresult=1;
/* 异常情况1:字符串为null */
if (string == null) checkresult=0;
int length = string.length(), offset = 0;
/* 异常情况2:字符串长度为0 */
if (length == 0) checkresult=0;
boolean negative = string.charAt(offset) == '-';
/* 异常情况3:字符串为'-' */
if (negative && ++offset == length) checkresult=0;
int result = 0;
char[] temp = string.toCharArray();
while (offset < length)
{
char digit = temp[offset++];
if (digit <= '9' && digit >= '0')
{
int currentDigit = digit - '0';
/*
* 异常情况4:已经等于Integer.MAX_VALUE / 10,推断要加入的最后一位的情况:
* 假设是负数的话,最后一位最大是8 假设是正数的话最后一位最大是7
* Int 范围:四个字节。-2147483648~2147483647
*/
if (result == Integer.MAX_VALUE / 10)
{
if ((negative == false && currentDigit > 7)
|| (negative && currentDigit > 8))
{
checkresult=0;
}
/*
* 异常情况5:已经大于Integer.MAX_VALUE / 10
* 不管最后一位是什么都会超过Integer.MAX_VALUE
*/
}
else if (result > Integer.MAX_VALUE / 10)
{
checkresult=0;
}
int next = result * 10 + currentDigit;
result = next;
}
}
return checkresult;
}
}
别人家的代码:
public class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;//负数不是回文
if(x==0) return true;
//对数值进行翻转,回文翻转之后还等于原来的数
int reverseNum=0;
int num=x;
while(num>0)
{
int modnum=num%10;
//考虑翻转会溢出的情况
if((reverseNum>Integer.MAX_VALUE/10)||((reverseNum==Integer.MAX_VALUE/10)&&(modnum>Integer.MAX_VALUE%10)))
return false;
reverseNum=reverseNum*10+modnum;
num=num/10;
}
if(reverseNum==x)
return true;
else
return false;
}
}
[LeetCode][Java] Palindrome Number的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- 【JAVA、C++】LeetCode 009 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...
- Java [leetcode 9] Palindrome Number
问题描述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
- 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. Do this without extra space. click to show spoilers. S ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- leetcode:Palindrome Number
Question: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Cou ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
随机推荐
- Java 遍历Map对象的4种方法
http://blog.csdn.net/tjcyjd/article/details/11111401
- java 利用Xstream注解生成和解析xml
https://www.oschina.net/code/snippet_116183_14202#23325
- duilib属性
原文转载自:http://blog.csdn.net/lixiang987654321/article/details/45008441 这里我想讲解一下duilib中的一些属性的理解,当然这是一篇永 ...
- WordPress个性页面制作教程
写在前面的话: 有很多WordPress小伙伴想制作不同风格的页面来满足自己的个性需求 但是大多数模板提供的页面模板非常有限,该如何手动制作属于自己风格的模板页呢? 其实,正如以上所说的,每个人都想拥 ...
- Elasticsearch--地理搜索
目录 地理位置索引 空间搜索映射定义 示例 基于距离的排序 边界框过滤 距离的限制 任意地理形状搜索 点 包络线 多边形 多个多边形 把形状保存到索引中 地理位置索引 空间搜索映射定义 elastic ...
- 面试中的一些小问题之ES5和ES6的区别?
1995年,JavaScript作为网景浏览器的一部分首次发布,起初并不叫JavaScript,而是叫LiveScript,但是因为当时Java正火,也算是为了搭上java的顺风车,于是改成了Java ...
- JavaScript开发心得--如何传递某行数据给下一页
1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...
- tp登陆注册(转)
登录时,更新用户数据,登录ip和登录时间,以及登录次数+1,此实现方便不知是否合适,待验证.源码地址:https://github.com/grh0812/thinkphp-login-registe ...
- window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory
今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpre ...
- 16Oracle Database 系统权限和对象权限
Oracle Database 系统权限和对象权限 Oracle中的系统权限和对象权限 DCL 数据控制语言 -- 查看对象的权限 grant / revoke 查看登录用户 Show user 查看 ...