PalindromeNumber之Java實(shí)現(xiàn)-創(chuàng)新互聯(lián)

一、題目

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?

在南岔等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作定制制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營銷型網(wǎng)站建設(shè),外貿(mào)營銷網(wǎng)站建設(shè),南岔網(wǎng)站建設(shè)費(fèi)用合理。

二、解題思路

1、此題為判斷一個(gè)數(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。

三、代碼實(shí)現(xiàn)

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享文章:PalindromeNumber之Java實(shí)現(xiàn)-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://muchs.cn/article20/dhdjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、全網(wǎng)營銷推廣、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化