Determine whether an integer is a palindrome. Do this without extra space.,palindrome
看到這個題目的時候,首先不認識 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 spacepublic 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 spacepublic 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;}}