劍指offerjava實現01

數組部分

聲明:本文內容的解題思路都是劍指offer第2版上的解題思路,只是我用java代碼實現,當然,也有理解有出入,不代表全部,還望理解。

1.1. 題目一:找出數組中重複的數字。

在一個長度爲n的數組裏的所有數字都在0~n-1的範圍內。數組中某些數字是重複的,但不知道有幾個數字重複了,也不知道每個數字重複了幾次。請找出數組中任意一個重複的數字。例如,如果輸入長度爲7的數組{2,3, 1,0,2,5,3},那麼對應的輸出是重複的數字2或者3。

源代碼地址:https://github.com/bubbletg/interviewSkills/blob/master/01_Array/src/cn/bubbletg/array/Array01.java

代碼

有點遺憾,沒能按照原書實現空間複雜度爲O(1).如果你知道怎麼實現歡迎留言告訴我(qq:363491343)


public class Array01 {
    public static void main(String[] args) {

        //輸入數據
        Scanner in = new Scanner(System.in);
        Integer n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        //輸出重複元素數組
        int[] duplication = new int[n];
        //返回輸出數組長度
        int length = 0;
        //數組長度爲空,或小於0,返回0
        if (arr.length <= 0) {
            System.out.println("數組長度大於0");
            return;
        }
        //數組內數字大於長度,返回0
        for (int i = 0; i < n; i++) {
            if (arr[i] < 0 || arr[i] > n - 1) {
                System.out.println("數組不合法");
                return;
            }
        }
        for (int i = 0; i < n; i++) {
            //數組的第i個值不等於下標,則交換
            while (arr[i] != i) {
                //
                if (arr[i] == arr[arr[i]]) {

                    //輸出數組長度加一

                    duplication[length++] = arr[i];
                    //跳出當前循環,不然進入死循環
                    break;
                }
                //交換
                int tmp = arr[i];
                arr[i] = arr[tmp];
                arr[tmp] = tmp;

            }
        }
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                System.out.printf("%d ", duplication[i]);
            }
        } else {
            System.out.println("沒有重複元素!");
        }

    }

}

1.2. 面試題4:二維數組中的查找

題目:在一個二維數組中,每一行都按照從左到右遞增的順序排序,每一列都按照從上到下遞增的順序排序。請完成一個函數, 輸入這樣的一個二維數組和一個整數,判斷數組中是否含有該整數。

源代碼地址:https://github.com/bubbletg/interviewSkills/blob/master/01_Array/src/cn/bubbletg/array/Array02.java

代碼


public class Array02 {
    public static void main(String[] args) {

        //輸入數據
        Scanner in = new Scanner(System.in);
        //輸入要查找的數
        int n = in.nextInt();
        int[][] arr = new int[][]{
                {1, 2, 8, 9},
                {2, 4, 9, 12},
                {4, 7, 10, 13},
                {6, 8, 11, 15}};

        //
        if (find(n, arr, 4, 4)) {
            System.out.println("存在數字" + n);
        } else {
            System.out.println("不存在");
        }
    }

    private static boolean find(int n, int[][] arr, int row, int line) {
        boolean b = false;

        if (arr != null && line > 0 && row > 0) {

            int row_ = 0;
            while (row > 0 && line > 0) {
                //判斷右上角是否相等,
                if (arr[row_][line - 1] == n) {
                    b = true;
                    System.out.println("數字 " + n + " :位置:(" + row_ + "," + (line - 1) + ")");
                    break;
                } else if (arr[row_][line - 1] < n) {
                    //在下方
                    row_++;
                    row--;
                } else if (arr[row_][line - 1] > n) {
                    //在前面
                    line--;
                }
            }
        }
        return b;
    }

}

1.3. 面試題5:替換空格

題目:請實現一個函數,把字符串中的每個空格替換成"%20"。例如,輸入“We are happy.", 則輸出“We%20are%20happy."。

源代碼地址:https://github.com/bubbletg/interviewSkills/blob/master/01_Array/src/cn/bubbletg/array/Array03.java

代碼

public class Array03 {
    public static void main(String[] args) {

        String chars = " We are happy.  ";
        //最簡單的做法,利用原生的api
        //System.out.println(chars.replace(" ", "%20"));

        //顯然,這裏不單單是考驗我們使用原生api


        System.out.println(replaces(chars, " ", "%20"));
    }

    /**
     * @param s           原字符串
     * @param target      被替換目標
     * @param replacement 替換的字符串
     * @return
     */
    public static String replaces(String s, String target, String replacement) {

        //計算空格數
        int k = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                k++;
            }
        }
        //計算替換字符串長度
        int l = replacement.length();

        //申請新的字符數組
        char[] chars = new char[s.length() + k * l];
        //指向空格前面的下標
        int q = s.length() - 1;
        //指向空格後的下標,因爲有k個空格,所以減k
        int p = s.length() + (k * l) - 1;
        //複製替換
        while (p >= 0 && q >= 0) {

            if (s.charAt(q) != ' ') {
                //向前移動並複製
                chars[p--] = s.charAt(q--);
            } else if (s.charAt(q) == ' ') {
                chars[p--] = '0';
                chars[p--] = '2';
                chars[p--] = '%';
                 --q;
            }
        }
        s = String.valueOf(chars);
        return s;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章