题目:

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的更多相关文章

  1. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. 【JAVA、C++】LeetCode 009 Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 解题思路一: 双指针法,逐位判断 Java代码如下 ...

  4. Java [leetcode 9] Palindrome Number

    问题描述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

  5. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  6. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  7. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  8. leetcode:Palindrome Number

    Question: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Cou ...

  9. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

随机推荐

  1. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  2. jQuery四叶草菜单效果,跟360杀毒软件差不多

    首先,我们要在js,css文件夹中创建js跟css,然后在body中写入html代码 <main><!--标签是 HTML 5 中的新标签. 素中的内容对于文档来说应当是唯一的.它不 ...

  3. 后台接收不到postman发送的xml参数的解决办法

    首先在body下复制需要传的xml: 然后点击url右边的Params,添加key和value.value和body下的xml是一样的: 最后点击send,后台就能接收到参数了.

  4. caffe2:conda路径和权限问题

    在使用conda之后,总是不能直接使用 conda install 命令,需要把codna添加到系统路径,取代默认Python. 在-/.bashrc中,添加 # added by Anaconda2 ...

  5. MFC_2.7 树控件的基本使用

    树控件的基本使用 1.添加控件设置变量绑定 2.添加数据 HTREEITEM RootNode1 = m_TreeCtrl.InsertItem(L"北京"); HTREEITEM ...

  6. jQuery,遍历表格每个单元格数据。

    <table class="table table-hover table-bordered"> <thead> <tr> <th > ...

  7. os下,vs code 自动编译ts

    nodejs安装ts npm install -g typescript 进入工程目录,用命令初始化ts(生成tsconfig.json) tsc --init 如果要指定生成目录,打开tsconfi ...

  8. Xamarin绑定ios静态库

    以下是官方的步骤介绍,我就不再一步步解释了 https://docs.microsoft.com/zh-cn/xamarin/ios/platform/binding-objective-c/walk ...

  9. 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

    package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...

  10. 使用lombok注解@Getter @Setter方法代码编译成功,但是没有生成get,set方法

    现象描述: 在对应类对象中,添加lombok的@Getter或@Setter注解,编译没有问题,但是在使用类对象时,没有出现对应的get或set方法. 配置且编译ok,但是没有对应的get或set方法 ...