Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:Coud you solve it without converting the integer to a string?
船營網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),船營網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為船營上千多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的船營做網(wǎng)站的公司定做!
1、此題為判斷一個數(shù)值是否是回文數(shù),即將數(shù)值反轉(zhuǎn)后是否等于原數(shù)值,如是,則為回文數(shù);
2、先判斷數(shù)值x是否小于0,如小于0則返回false;
3、再判斷x是否等于0,如是,則返回true;
4、如上面兩種情況都不符合,則將數(shù)值x循環(huán)取余取出每一位上的數(shù)存入集合中;
5、循環(huán)將集合中的數(shù)取出,求得反轉(zhuǎn)后的數(shù)值;
6、與數(shù)值x比較是否相等,如是,則返回true,否則返回false。
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
} else if (x == 0) {
return true;
} else {
int temp = x;
int result = 0;
List<Integer> list = new ArrayList<Integer>();
while (temp != 0) {
list.add(temp % 10);
temp = temp / 10;
}
for (int i = 0; i < list.size(); i++) {
result = result + list.get(i) * (int)Math.pow(10, list.size() - 1 - i);
}
if (result == x) {
return true;
} else {
return false;
}
}
}
文章標題:PalindromeNumber之Java實現(xiàn)
文章鏈接:http://chinadenli.net/article26/gjeijg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、定制開發(fā)、外貿(mào)網(wǎng)站建設(shè)、電子商務(wù)、App設(shè)計、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)