算法题都是有趣的,就像爬山,一开始痛苦,到山顶之后就豁然开朗。
这一题是对回文数字的判断,Java解的,时间复杂度稍微有点高,可以参考看看。
class Solution {
public static boolean isPalindrome(int testNumber) {
int info = testNumber;
if(testNumber<0) return false;
if(testNumber==0) return true;
int left = 0;
int changedNumber = 0;
int numberlength = getNumberLength(testNumber)-1;
while(testNumber!=0) {
left = testNumber % 10;
testNumber = testNumber / 10;
changedNumber +=(int) (left * Math.pow(10, numberlength--));
}
System.out.println(changedNumber);
return info==changedNumber;
}
public static int getNumberLength(int number) {
int length = 0;
while(number!=0) {
number = number/10;
//System.out.println(number);
length++;
}
return length;
}
}