第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

回文檢查 Java 中 Int 的效率

回文檢查 Java 中 Int 的效率

猛跑小豬 2022-03-10 16:10:34
我已經(jīng)構(gòu)建了兩種檢查數(shù)字回文的方法。哪個(gè)效率更高?我所說(shuō)的效率是指執(zhí)行時(shí)間和內(nèi)存分配。首先,我將整數(shù)轉(zhuǎn)換為字符串并檢查它是否是回文。代碼示例如下。public class Palindrome{/*Function palindromeCheckReturn type booleanParameters characterArrayChecks character array for palindrome*/ public static boolean palindromeCheck(char[] palinCheck){    boolean palindrome = true;    int firstLen = 0;    int secondLen = palinCheck.length - 1;    while(palindrome == true && firstLen < secondLen ){        if(palinCheck[firstLen] != palinCheck[secondLen]){            palindrome = false;        }        else{            firstLen++;            secondLen--;        }    } //end of while    return palindrome;} /*Main FunctionCalls palinDromeCheck functionPrints results*/public static void main(String[] args){    int palinCheck = 1221;    String dipendra = Integer.toString(palinCheck);    char[] dipendraChar = dipendra.toCharArray();    System.out.println(palindromeCheck(dipendraChar));}}第二種方法是不將其轉(zhuǎn)換為字符串。public class PalindromeNumber{/*    Function: PalindromeCheck    parameters integer    ReturnType: boolean    Takes integer, checks if it is palindrome and returns accordingly*/public static boolean palindromeCheck(int number){    int firstNumber = number;    int secondNumber = 0;    while(number >= 1){        secondNumber = secondNumber* 10 + (number%10);        number = number/10;    }    return (firstNumber==secondNumber) ? true:false;}public static void main(String[] args){    System.out.println(palindromeCheck(111));}}
查看完整描述

3 回答

?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

我敢打賭第二種算法會(huì)更快,而且顯然更節(jié)省空間。如果您假設(shè)n是輸入數(shù)字的位數(shù),則在第一個(gè)算法中:

  • Integer.toString需要n 個(gè)步驟才能將其轉(zhuǎn)換為String.

  • palindromeCheck需要n / 2 次比較來(lái)檢查它是否是回文。

但是,第二種算法需要n 個(gè)步驟來(lái)計(jì)算倒數(shù)(僅涉及整數(shù)運(yùn)算)并且只需要 1 個(gè)比較來(lái)檢查。


查看完整回答
反對(duì) 回復(fù) 2022-03-10
?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

我們?cè)囋嚢?。在以下示例中(使用一個(gè)特定數(shù)字,在我的特定機(jī)器上......):

  • 580 毫秒 - 您的第一個(gè)解決方案

  • 323 毫秒 - 您的第二個(gè)解決方案

  • 1045 毫秒 - BrentR 的解決方案

注意我修改了代碼(但不是邏輯)。您還應(yīng)該注意空格和縮進(jìn)。

public class Palindrome {


    public static boolean isPalindrome1(int n) {

        char a[] = Integer.toString(n).toCharArray();

        int i = 0;

        int j = a.length - 1;


        while (i < j) {

            if (a[i++] != a[j--]) return false;

        }

        return true;

    }


    public static boolean isPalindrome2(int n) {

        int p = n, q = 0;


        while (n > 0) {

            q = 10 * q + n % 10;

            n /= 10;

        }

        return p == q;

    }


    public static boolean isPalindrome3(int n) {

         String s = Integer.toString(n);

         return s.equalsIgnoreCase(new StringBuilder(s).reverse().toString());

    }


    public static void main(String[] args) {

        final int m = 10000000;

        long t1, t2;

        boolean q;


        t1 = System.currentTimeMillis();

        for (int n = 0; n < m; n++) {

            q = isPalindrome1(123454321);

        }

        t2 = System.currentTimeMillis();

        System.out.println(t2 - t1);


        t1 = System.currentTimeMillis();

        for (int n = 0; n < m; n++) {

            q = isPalindrome2(123454321);

        }

        t2 = System.currentTimeMillis();

        System.out.println(t2 - t1);


        t1 = System.currentTimeMillis();

        for (int n = 0; n < m; n++) {

            q = isPalindrome3(123454321);

        }

        t2 = System.currentTimeMillis();

        System.out.println(t2 - t1);


    }

}


查看完整回答
反對(duì) 回復(fù) 2022-03-10
?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

你為什么要重新發(fā)明輪子?


java.lang.StringBuilder 已經(jīng)提供了字符串反轉(zhuǎn)方法


String string = Integer.toString(10101);


boolean palindrome = string.equalsIgnoreCase(new StringBuilder(string).reverse().toString());



查看完整回答
反對(duì) 回復(fù) 2022-03-10
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)