在不使用额外的内存空间的条件下判断一个整数是否是回文。
回文指逆序和正序完全相同。
数据范围:
进阶: 空间复杂度
,时间复杂度
提示:
负整数可以是回文吗?(比如-1)
如果你在考虑将数字转化为字符串的话,请注意一下不能使用额外空间的限制
你可以将整数翻转。但是,如果你做过题目“反转数字”,你会知道将整数翻转可能会出现溢出的情况,你怎么处理这个问题?
public boolean isPalindrome (int x) {
// write code here
int num=0;
while(x>num){
num=num*10+x%10;
x/=10;
if(num==x || num==x/10){
return true;
}
}
return x==0;
} import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
//1 2 3
//1 2 3 4
String str=String.valueOf(x);
for(int i=0;i<str.length()/2;i++){
char a=str.charAt(i);
char b=str.charAt(str.length()-i-1);
if(a!=b){
return false;
}
}
return true;
}
} import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
// write code here
int tag = 1;
if (x < 0) {
tag = -1;
x = -x;
}
int a = x, b = 0;
while (a != 0) {
b = b * 10 + a % 10;
a = a / 10;
}
return x == b * tag ? true : false;
}
} 不如来个简单的思路
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
// write code here
String r= String.valueOf(x);
StringBuilder sb = new StringBuilder();
sb.append(r.toCharArray());
return sb.reverse().toString().equals(r);
}
} import java.util.*;
public class Solution {
public boolean isPalindrome (int x) {
if (x < 0) return false;
int y = 0;
int z = x;
while (!(z / 10 == 0)) {
y = (z % 10 + y) * 10;
z = z / 10;
}
if (x / 10 * 10 == y) return true;
return false;
}
} import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
if(x<0) return false;
if(x<10) return true;
if(x%10==0) return false;
return x==reverse(x,0);
}
public int reverse(int x,int pre){
if(x<10) return x+pre*10;
return reverse(x/10,x%10+pre*10);
}
} import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
// write code here
if(x < 0 ) return false;
//计算最高位次
int high = 1;
long temp = 10;
while(x/temp != 0 ){
high ++;
temp*=10;
}
//i每次加2 因为每次消除两位
for(int i = 1 ; i <= high/2;i+=2){
int left = (int)(x%Math.pow(10,1));
int right = (int)(x/Math.pow(10,high-i));
if( left!=right )
return false;
//最高位
int max = (int)(Math.pow(10,high-i));
//消除最高位
x = x % max;
//消除最低位
x = x/10;
}
return true;
}
} public synchronized StringBuffer append(int i){ toStringCache = null; super.append(i); return this; } public boolean isPalindrome (int x) { StringBuffer sb = new StringBuffer().append(x); String bs = sb.reverse().toString(); if (bs.equals(String.valueOf(x))) { return true; } else { return false; } }
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
// write code here
//利用指针进行查找
String str= String.valueOf(x);
int begin=0;
int end=str.length()-1;
while(begin<=end){
//如果相等,遍历下一个
if(str.charAt(begin)==str.charAt(end)){
begin++;
end--;
}else{
return false;
}
}
return true;
}
} public boolean isPalindrome (int x) {
return String.valueOf(x).equals(new StringBuilder(String.valueOf(x)).reverse().toString());
} import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
if(x<0) return false;
int len=0;
int tmp = x;
//计算一共是几位数
while(tmp!=0){
tmp = tmp/10;
len++;
}
//通过运算符模拟双指针比较数字
for(int i=0; i<len; i++) {
int left = x/(int)Math.pow(10,len-i-1)%10;
int right = x%(int)Math.pow(10,i+1)/(int)Math.pow(10,i);
if(left!=right) return false;
}
return true;
}
} import java.util.*;
public class Solution {
public boolean isPalindrome (int x) {
if (x > 0) {
boolean flag = false;
StringBuffer nx = new StringBuffer(String.valueOf(x));
StringBuffer nnx = new StringBuffer(String.valueOf(x));
nx.reverse();
int s1 = Integer.parseInt(nx.toString());
int s2 = Integer.parseInt(nnx.toString());
return s1 == s2 ? true : false;
}else if( x ==0){
return true;
} else {
return false;
}
}
}
请问一下,我这个方法有一个用例没过,该怎么解决?2147483647 输入这个数字失败了