Java日記2018-05-13

第一題 最小的 K 個(gè)數(shù)
可以基于Partition函數(shù)來(lái)解決這個(gè)問題。如果基于數(shù)組的第k個(gè)數(shù)字來(lái)調(diào)整,使得比第k個(gè)數(shù)字小的所有數(shù)字都位于數(shù)組的左邊,比第k個(gè)數(shù)字大的所有數(shù)字都位于數(shù)組的右邊。這樣調(diào)整之后,位于數(shù)組中左邊的k個(gè)數(shù)字就是最小的k個(gè)數(shù)字;
Partition 來(lái)源于快速排序思想.
觀察快速排序是把全部數(shù)組排序,當(dāng)前值需要前k個(gè)就行,所以加上對(duì)于index與k的判斷,只要前k個(gè)

package com.lyc.dataautest;



    
    
    public static int partion2(int[] arr,int start,int end) {
        if(arr==null&&start>end) return -1;

        int res = arr[start];
        while(start<end) {
            while(start<end&&arr[end] >= res) {
                end--;
            }
            arr[start] = arr[end];
            while(start<end && arr[start]<res) {
                start++;
            }
            arr[end] = arr[start];
        }
        
        //初始值給與當(dāng)前最左邊的數(shù)字,使得當(dāng)前數(shù)組所有數(shù)字包含原數(shù)組
        arr[start] = res;

        return start;
    }
    public static int[] getLeastNumbers2(int[] input, int k) {
    
        if (input.length == 0 || k <= 0)
            return null;
        int[] output = new int[k];
        int start = 0;
        int end = input.length - 1;
        int index = partion2(input, start, end);
        
        while(index != k - 1) {
            if(index>k-1) {
                end = index-1;
                index = partion2(input, start, end);
            } else {
                start = index+1;
                index = partion2(input, start, end);
            }
            
        }
        
        for (int i = 0; i < k; i++) {
            output[i] = input[i];
            System.out.print(output[i] + " ");
        }
        // System.out.println("end print2");
        return output;
    }

    
    //快速排序
    public static void sort_fast(int[] arr,int left,int right) {
        if(left>right) return;
        
        int i= partion2(arr,left,right);
        sort_fast(arr,left,i-1);
        sort_fast(arr,i+1,right);
        
    }
    public static void main(String[] args) {
        int[] arr = { 4, 5, 1, 6, 2, 7, 3, 8 };
        //getLeastNumbers2(arr, 4);
        sort_fast(arr,0,arr.length-1);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        
    }

}

第二題 數(shù)據(jù)流中的中位數(shù)
參考 https://blog.csdn.net/u011080472/article/details/51291089

考慮將數(shù)據(jù)序列從中間開始分為兩個(gè)部分,左邊部分使用大根堆表示,右邊部分使用小根堆存儲(chǔ)。每遍歷一個(gè)數(shù)據(jù),計(jì)數(shù)器count增加1,當(dāng)count是偶數(shù)時(shí),將數(shù)據(jù)插入小根堆;當(dāng)count是奇數(shù)時(shí),將數(shù)據(jù)插入大根堆。當(dāng)所有數(shù)據(jù)遍歷插入完成后(時(shí)間復(fù)雜度為O(logn)O(logn),如果count最后為偶數(shù),則中位數(shù)為大根堆堆頂元素和小根堆堆頂元素和的一半;如果count最后為奇數(shù),則中位數(shù)為小根堆堆頂元素。

下午重來(lái)

public class Solution {
    // 大頂堆,存儲(chǔ)左半邊元素
    private PriorityQueue<Integer> left = new PriorityQueue<>((o1, o2) -> o2 - o1);
    // 小頂堆,存儲(chǔ)右半邊元素,并且右半邊元素都大于左半邊
    private PriorityQueue<Integer> right = new PriorityQueue<>();
    // 當(dāng)前數(shù)據(jù)流讀入的元素個(gè)數(shù)
    private int N = 0;

    public void Insert(Integer val) {
        // 插入要保證兩個(gè)堆存于平衡狀態(tài)
        if (N % 2 == 0) {
            // N 為偶數(shù)的情況下插入到右半邊。
            // 因?yàn)橛野脒呍囟家笥谧蟀脒叄切虏迦氲脑夭灰欢ū茸蟀脒呍貋?lái)的大,
            // 因此需要先將元素插入左半邊,然后利用左半邊為大頂堆的特點(diǎn),取出堆頂元素即為最大元素,此時(shí)插入右半邊
            left.add(val);
            right.add(left.poll());
        } else {
            right.add(val);
            left.add(right.poll());
        }
        N++;
    }

    public Double GetMedian() {
        if (N % 2 == 0)
            return (left.peek() + right.peek()) / 2.0;
        else
            return (double) right.peek();
    }
}

第三題 字符流中第一個(gè)不重復(fù)的字符

因?yàn)橐粋€(gè)字符不會(huì)超過(guò)8位,所以創(chuàng)建一個(gè)大小為256的整形數(shù)組,創(chuàng)建一個(gè)List,存放只出現(xiàn)一次的字符。insert時(shí)先對(duì)該字符所在數(shù)組位置數(shù)量+1,再判斷該位置的值是否為1,如果為1,就添加到List中,不為1,則表示該字符已出現(xiàn)不止1次,然后從List中移除,取出時(shí)先判斷List的size是否為0,不為0直接List.get(0),就可以得到結(jié)果,否則返回‘#’

package com.lyc.dataautest;

import java.util.ArrayList;

public class FirstAppearingOnce {
    private int[] chaCnt = new int[256];
    private ArrayList<Character> lst = new ArrayList<Character>();

    public void insertchar(char ch) {
        chaCnt[ch]++;
        if (chaCnt[ch] == 1) {
            lst.add(ch);
        } else {
            // 當(dāng)前的數(shù)組是否已經(jīng)有重復(fù)的刪除,注意要將Character奉上
            lst.remove((Character)ch);
        }
    }

    public char findFist() {
        if (lst.size() == 0) {
            return '#';
        } else {

            return lst.get(0);
        }
    }

    public static void main(String[] args) {
        StringBuffer stb = new StringBuffer("google");
        FirstAppearingOnce fao = new FirstAppearingOnce();
        for (int i = 0; i < stb.length() - 1; i++) {
            fao.insertchar(stb.charAt(i));
        }

        for (int i = 0; i < fao.lst.size() - 1; i++) {
            System.out.println(fao.lst.get(i));
        }

        System.out.println(fao.findFist());
    }

}

第四題 連續(xù)子數(shù)組的最大和

首先想到用動(dòng)態(tài)規(guī)劃算法,設(shè)置兩個(gè)變量,一個(gè)currentMax用來(lái)記錄數(shù)組相加之和,一個(gè)sumMax用來(lái)記錄最大的子數(shù)組和,一旦currentMax大于sumMax,就更新sumMax使它等于currentMax;初始值都賦予數(shù)組的第一個(gè)元素的值;
那么問題來(lái)了,為什么最大的子數(shù)組為0來(lái)作為判斷,為啥不能是1,哥沒想太明白,迷迷糊糊懂……

package com.lyc.dataautest;

public class FindGreatestSumOfSubArray {
    public static int findmax(int[] arr) {
        if(arr==null) return 0;
        int cmax=arr[0];
        int res = arr[0];
        //注意循環(huán)從i=1開始
        for(int i=1;i<arr.length;i++) {
            if(cmax<0) {
                cmax=arr[i];
            } else {
                cmax+=arr[i];
            }
            if(cmax>res) {
                res = cmax;
            }
        }
        return res;
    }
    
    public static void main(String[] args) {
        int[] arr={6,-3,-2,7,-15,1,2,2};
        int te = findmax(arr);
        System.out.println("the res is:"+te);
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容