Java日記2018-05-30

  1. 數(shù)值的整數(shù)次方
    遞歸一定不能忘了寫遞歸停止的判斷,另外還要注意expon是負(fù)值的情況(此處沒考慮)
public static double Power(double base, int expon) {
        // 注意遞歸停止的判斷
        if (expon == 0)
            return 1;
        if (expon == 1)
            return base;
        double pow = Power(base * base, expon / 2);
        if (expon % 2 != 0) {
            pow = pow * base;
        }
        return pow;
    }
  1. 打印從 1 到最大的 n 位數(shù)
    以n=2為例,寫成一個字符串s[i][j],先打印s[i],然后打印s[i][j],所以實際需要兩個循環(huán),先是s[0][j],然后是s[i][j],然后遞歸實現(xiàn)。
    回頭一看,解題思路寫的邏輯真混亂啊,但是也不知道怎么改,還是寫具體實現(xiàn)……
//n的最大數(shù)打印
    public static void printMax(int n) {
        if(n<1) return;
        StringBuffer s= new StringBuffer(n);
        for(int i=0;i<n;i++) {
            s.append('0');
        }
        
        for(int i=0;i<n;i++) {
            s.setCharAt(0, (char)(i+'0'));
            printMaxcore(n,s,0);
        }
    }
    public static void printMaxcore(int n,StringBuffer s,int index) {
        if(n==index+1){
            prints(s);
            return;
        }
        for(int i=0;i<10;i++) {
            s.setCharAt(index+1, (char)(i+'0'));
            printMaxcore(n,s,index+1);
        }
        
    }
    public static void prints(StringBuffer s) {
        //從s[i][j]的左到右依次檢查是否為0,感覺當(dāng)前實現(xiàn)太繞,可以優(yōu)化
        boolean isBeginning0 = false;
        for(int i=0;i<s.length();i++) {
            if (s.charAt(i) != '0') {
                isBeginning0 = true;
            }
            if (isBeginning0) {
                System.out.print(s.charAt(i));
            }
        }
        System.out.println();
        
    }

18.1 在 O(1) 時間內(nèi)刪除鏈表節(jié)點
看一下注釋

public static ListNode deleteNode(ListNode head, ListNode tobeDelete) {
        if(head==null || head.next==null || tobeDelete == null) return null;
        if(tobeDelete.next!=null) {
            //刪除節(jié)點不是尾節(jié)點,則將刪除節(jié)點的下一個節(jié)點賦值給要刪除的節(jié)點,節(jié)點指向下一個的下一個節(jié)點
            ListNode next=tobeDelete.next;
            tobeDelete.val=next.val;
            tobeDelete.next= next.next;
        } else {
            ListNode cur=head;
            while(cur.next != tobeDelete){
                cur=cur.next;
            }
            cur.next=null;
        }
        return head;
    }

18.2 刪除鏈表中重復(fù)的結(jié)點
看著簡單,其實有陷阱

//刪除重復(fù)節(jié)點
    public static ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null || pHead.next == null)
            return pHead;
        ListNode next = pHead.next;
        //如果頭以及頭下一個節(jié)點相等,則需要返回不相等的節(jié)點作為頭;否則可以返回當(dāng)前頭作為頭,頭的下一個節(jié)點在刪除后的頭節(jié)點
        if (pHead.val == next.val) {
            while (next != null && pHead.val == next.val)
                next = next.next;
            return deleteDuplication(next);
        } else {
            pHead.next = deleteDuplication(pHead.next);
            return pHead;
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第2章 基本語法 2.1 概述 基本句法和變量 語句 JavaScript程序的執(zhí)行單位為行(line),也就是一...
    悟名先生閱讀 4,557評論 0 13
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,692評論 18 399
  • 題目 男人八題 之 Connected Graph題目內(nèi)容可以看上邊鏈接,這里不在贅述。就是求 n 個不同點的無向...
    86棵夢閱讀 1,143評論 0 0
  • 今天我讀完了一本書,這本書的名字叫做龍灣謎蹤,我再給大家大致講一下他的故事內(nèi)容。首先發(fā)生的事情就是一個團(tuán)隊...
    子細(xì)閱讀 102評論 0 1
  • 整體解釋 ——陳嘉映《哲學(xué) 科學(xué) 常識》讀書筆記(50) 王曉春 ……理論的系統(tǒng)性突...
    讓教育充滿智慧閱讀 373評論 0 0

友情鏈接更多精彩內(nèi)容