[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 ...
随机推荐
- hibernate关联关系查询
关联关系 一对一 A中包含B的对象,B中包含A的对象 一对多 A中包含B的集合,B中包含A的对象 多对多 A中包含B的集合,B中包含A的集合 1,一对多配置 一名老师可以对应多名学生 2,模型类 老师 ...
- CF817B Makes And The Product
思路: 模拟,数学. 实现: #include <iostream> #include <cstdio> #include <algorithm> using na ...
- protobuf的lua版
推荐个protobuf的lua版 以前项目客户端lua,通信协议是protobuf,用网易的proto-gen-lua,使用过程遇到些问题需要绕,比如: 1.每次更改.增加proto都要生成新 ...
- codeforces_305C_STLset
C. Ivan and Powers of Two time limit per test 0.5 seconds memory limit per test 256 megabytes input ...
- css3文字渐变无效果的解决方案
现在css3越来月流行了,为了实现一些高大上的效果,我们会用一些渐变的特效,请看文字渐变的特效代码: .title { font-size: 60px; line-height: 80px; text ...
- 剑指offer---圆圈中最后剩下的数
题目:圆圈中最后剩下的数 要求:0,1,2...n-1 共n个数排成一个圆圈,从数字0开始,每次删除第m个元素,求这个圆圈里面剩下的最后一个元素 如 n=5, m=3 的情况:0, 1, 2, 3, ...
- Python学习第二阶段day1 内置函数,序列化,软件目录开发规范
内置函数 1.abs() 求绝对值 2.all() 所有元素为真才返回真 all( [1,1,2,3,-1] ) 值为True 3.any() 所有元素为假才返回假 any([0,0,0 ...
- eclipse如何能快速找到某个文件夹或者文件
eclipse如何能快速找到某个文件夹或者文件 直接在editor中右键 Show in -> Package Explorer 在package explore 中选中link就可以了 (魔芋 ...
- java增强for循环中获取index
java增强for循环中获取index http://rensanning.iteye.com/blog/2003205
- nyoj 95 众数问题(set)
众数问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重 ...