看到这个题目的时候,首先不认识 Determine这个单词,英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思。
问题是 回文是个什么东西,官方解释: A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. 回文
虽然英文不好,但是这个英文解释还是看懂了的。意思就是从前读到后面和倒过来读是一样的。
然后又不理解后面那句 do this without extra space. 大概意思是实现的时候不能使用其他空间,其实还是不懂。
不知道第二个方法里的,Math.pow()这个方法的调用算不算使用其他空间。
public class palindrome {
//using with extra space
public static boolean check(int x){
String temp = Integer.toString(x);
boolean flag = true;
for(int i=0;i<temp.length()/2;i++){
char a = temp.charAt(i);
char b = temp.charAt(temp.length()-i-1);
if(a!=b){
flag = false;
}
}
return flag;
}
//using without extra space
public static boolean check2(int x){
if(x<0)
return false;
int n=1;
int temp = x;
while(temp/10!=0){
temp=temp/10;
n++;
}
for(int i=0;i<n/2;i++){
int a = i;
int b = n-1-i;
if(getInt(x,a)!=getInt(x,b)){
return false;
}
}
return true;
}
// 比如 896698 这个数字,要获取百位,用896698除以100,得到8966然后取余10得到6,即为百位的数值
private static int getInt(int x,int i){
int a = (int) Math.pow(10, i);
return (x/a)%10;
}
}