1. 程式人生 > 其它 >劍指offer 6~9

劍指offer 6~9

6.劍指 Offer 11. 旋轉陣列的最小數字

二分查詢

class Solution {
     public int minArray(int[] numbers) {
        int l = 0, r = numbers.length - 1;
        while (l < r) {
            int mid = ((r - l) >> 1) + l;
            //只要右邊比中間大,那右邊一定是有序陣列
            if (numbers[r] > numbers[mid]) {
                r = mid;
            } else if (numbers[r] < numbers[mid]) {
                l = mid + 1;
             //去重
            } else r--;
        }
        return numbers[l];
    }
}

2.劍指 Offer 12. 矩陣中的路徑

給定一個m x n 二維字元網格board 和一個字串單詞word 。如果word 存在於網格中,返回 true ;否則,返回 false 。

單詞必須按照字母順序,通過相鄰的單元格內的字母構成,其中“相鄰”單元格是那些水平相鄰或垂直相鄰的單元格。同一個單元格內的字母不允許被重複使用。

例如,在下面的 3×4 的矩陣中包含單詞 "ABCCED"(單詞中的字母已標出)。

回溯

class Solution {
    public boolean exist(char[][] board, String word) {
        if(board == null || board.length ==0 || board[0].length == 0){
            return false;
        }
        char[] chars = word.toCharArray();
        boolean[][] visited = new boolean[board.length][board[0].length];
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(dfs(board,chars,visited,i,j,0)) return true;
            }
        }
        return false;
    }

    private boolean dfs(char[][] board,char[] chars,boolean[][] visited,int i,int j,int start){
        if(i<0 || i>= board.length || j<0 || j>= board[0].length || chars[start] != board[i][j] || visited[i][j]){
            return false;
        }
        //遞迴終止條件
        if(start == chars.length - 1) return true;
        //設定為使用過
        visited[i][j] = true;
        boolean ans = false;
        //以board[i][j]為中心,上下左右遞迴
        ans = dfs(board,chars,visited,i+1,j,start + 1)
        || dfs(board,chars,visited,i-1,j,start + 1)
        || dfs(board,chars,visited,i,j+1,start + 1)
        || dfs(board,chars,visited,i,j-1,start + 1);

        //釋放
        visited[i][j] = false;
        return ans;
    }
}

3.劍指 Offer 05. 替換空格

class Solution {
    public String replaceSpace(String s) {
        int length = s.length();
        char[] array = new char[length * 3];
        int size = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                array[size++] = '%';
                array[size++] = '2';
                array[size++] = '0';
            } else {
                array[size++] = c;
            }
        }
        String newStr = new String(array, 0, size);
        return newStr;
    }
}
class Solution {

    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        return dfs(0,0,m,n,k,visited);
    }

    private int dfs(int i,int j,int m,int n,int k,boolean visited[][]){
        if(i<0 || i>=m || j<0 || j>= n || (i/10 + i%10 + j/10 + j%10) > k || visited[i][j]){
            return 0;
        }
        visited[i][j] = true;
        //遞迴下邊的和右邊的
        return dfs(i + 1, j, m, n, k, visited) + dfs(i, j + 1, m, n, k, visited) + 1;
    }

}

4.劍指 Offer 06. 從尾到頭列印連結串列

class Solution {
    public static int[] reversePrint(ListNode head){
        ListNode node = head;
        int count = 0;
        while(node != null){
            ++count;
            node  = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for(int i = count-1;i>=0;--i){
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }
}