Determine whether an integer is a palindrome. Do this without extra space.,palindrome

來源:互聯網
上載者:User

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;}}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.