[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 ...
随机推荐
- MacOS 下安装 MySQL8.0 登陆 MySQL
按照 官方教程 ,下载安装包,点击安装后,如需在命令行启动,还需设置命令路径: 在命令行中,打开配置文件 .bash_profile: vim ~/.bash_profile 在最后一行加上: PAT ...
- 重新学习Java——Java基本的程序设计结构(二)
上一节简单回顾了Java基本的一些程序设计的知识,这一节将继续根据<Java核心技术>这本书,进行这方面知识的复习与探索. 1. 字符串 Java字符串实际上就是Unicode字符序列.例 ...
- css3 动画 vs js 动画
之前被问到过,css3 动画与 js 动画孰优孰劣,脑袋的第一反应就是性能上肯定 css3 动画会好很多,但别人说不对,我就在想,不对?难道还有别的原因吗?答案是肯定的.先来看看二者实现动画的原理吧. ...
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- spring源码下载链接
http://www.blogjava.net/zhyiwww/archive/2014/10/17/418809.html
- 常用HTML5代码片段
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 总结在Linux终端中进行算术运算的6种方式
1.使用bash 使用双括号可以像C语言一样直接使用运算符进行计算. +)) a=$((*)) echo $a b=$(($a-)) echo $b d=$(($b/)) echo $d e=$(($ ...
- buf.write()
buf.write(string[, offset[, length]][, encoding]) string {String} 需要被写入到 Buffer 的字节 offset {Number} ...
- mac下用crontab实现pytho3脚本自动定期执行,包括scrapy的定期执行
呃 其实要明天上午才能知道是否成功,毕竟改了一个小参数的. 首先,来学两个小命令: step1: $ sudo crontab -e step2: # 然后提示password输入密码,即可进入编辑页 ...
- springcloud(十二):Ribbon客户端负载均衡介绍
springcloud(十二):Ribbon客户端负载均衡介绍 Ribbon简介 使用分布式微服务脚骨的应用系统,在部署的时候通常会为部分或者全部微服务搭建集群环境,通过提供多个实例来提高系统的稳定型 ...