[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 ...
随机推荐
- centos服务器/dev/xvda1空间占满的解决方法
突然线上Centos的机器磁盘空间占满报警,第一反映是日志文件很大,占用了较多的磁盘空间.于是简单的上去看了一下.但是发现线上不是的地址对应的空间占的并不多.用:df -h 命令看了一下,/dev/x ...
- mac当你有多个版本的命令存在是怎么使用最新版本
例如你安装了一个最新的git.然而系统中由于xcode等自带的git的存在.使得/usr/bin/git 是xcode的版本. 只需要再 ~/.bash_profile 中添加一行优先path即可 e ...
- 百度地图API在vue-cli中路径错误的问题
在使用百度地图的时候,需要使用自定义的icon图片,百度的案例中使用的是线上地址,但当替换为本地图片路径的时候,错误出现了 这是本地图片地址 ) // 设置覆盖物大小 ); 这里有一点需要注意,这里路 ...
- React Native组件的结构和生命周期
React Native组件的结构和生命周期 一.组件的结构 1.导入引用 可以理解为C++编程中的头文件. 导入引用包括导入react native定义的组件.API,以及自定义的组件. 1.1 导 ...
- 外观模式(Facade)-子系统的协作与整合-接口模式
对子系统进行整合,对外提供更强大或更便捷的接口. 在一个模块和几个子系统进行通信时考虑. 什么是外观模式? 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口 ...
- Django框架 之基础入门
django是一款MVT的框架 一.基本过程 1.创建项目:django-admin startproject 项目名称 2.编写配置文件settings.py(数据库配置.时区.后台管理中英文等) ...
- CAD使用SetxDataDouble写数据(网页版)
主要用到函数说明: MxDrawEntity::SetxDataDouble 写一个Double扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据 ...
- 梦想CAD控件事件COM接口知识点
一.鼠标事件 _DMxDrawXEvents::MouseEvent 控件中的鼠标事件. 参数 说明 LONG lType 事件类型,1鼠标移动,2是鼠标左键按下,3是鼠标右键按下,4是鼠标左键双击 ...
- 洛谷——P1404 平均数
P1404 平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 前缀和+二分答案 #include<iostream> ...
- Python学习第二阶段Day2,模块time/datetime、random、os、sys、shutil
1.Time. Datetime(常用) UTC时间:为世界标准时间,时区为0的时间 北京时间,UTC+8东八区 import time print(time.time()) # timestamp ...