DFS

rand5() 隨機產生 1,2,3,4,5 rand5() - 1隨機產生 0,1,2,3,4 (rand5() -1) * 5 隨機產生 0,5,10,15,20 (rand5() - 1) * 5 + (rand5() - 1) 隨機產生 0,1,2,3....,23,24 插空過程java

public int rand5() {
    return (int) (Math.random() * 5) + 1;
}

public int rand7() {
    int num = 0;
    do {
        num = (rand5() - 1) * 5 + rand5() - 1;
    } while (num > 20);
    return num % 7 + 1;
}
複製代碼

Path Sumnode

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        
        //end of the recursion: reach the leaf node && the sum value is 0
        if (sum == root.val && root.left == null && root.right == null) {
            return true;
        }
        
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}
複製代碼

Permutationdom

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        boolean[] visited = new boolean[nums.length];
        helper(nums, res, new ArrayList<>(), visited);
        return res;
    }
    
    private void helper(int[] nums, List<List<Integer>> res, List<Integer> level, boolean[] visited) {
        if (level.size() == nums.length) {
            res.add(new ArrayList(level));
            return;
        }
        
        for (int i = 0; i < nums.length; i++) {
            if (visited[i]) {
                continue;
            }
            level.add(nums[i]);
            visited[i] = true;
            helper(nums, res, level, visited);
            visited[i] = false;
            level.remove(level.size() - 1);
        }
    }
}

複製代碼

Friend Circlespa

public class Solution {
    public void dfs(int[][] M, int[] visited, int i) {
        for (int j = 0; j < M.length; j++) {
            // 對於每個friend都作DFS並標記爲1,表示已經能夠經過某個朋友圈找到
            if (M[i][j] == 1 && visited[j] == 0) {
                visited[j] = 1;  
                dfs(M, visited, j);
            }
        }
    }
    public int findCircleNum(int[][] M) {
        int[] visited = new int[M.length];
        int count = 0;
        for (int i = 0; i < M.length; i++) {
            if (visited[i] == 0) {
                dfs(M, visited, i);
                count++;
            }
        }
        return count;
    }
}
複製代碼

Surrounded Regioncode

  • 問題: 由OX組成的矩陣,找出全部被X包圍的O,並將其翻轉爲O
  • 思路: 全部在邊界上的O都是不可能被X徹底包圍的點,將這些點做爲起點DFS搜索
class Solution {
    public void solve(char[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        int rows = board.length, cols = board[0].length;
        // starting from all O in the top/bottom border
        for (int i = 0; i < cols; i++) {
            if (board[0][i] == 'O') {
                dfs (board, 0, i);
            }
            if (board[rows-1][i] == 'O') {
                dfs (board, rows-1, i);
            }
        }
        // start from all O in the left/right board
        for (int i = 0; i < rows; i++) {
            if (board[i][0] == 'O') {
                dfs (board, i, 0);
            }
            if (board[i][cols-1] == 'O') {
                dfs (board, i, cols-1);
            }
        }
        // finally, rescan every spot, convert all O left to X, all 1 to O
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (board[i][j] == '1') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }

    private void dfs(char[][] board, int x, int y) {
        // mark current index as visited
        board[x][y] = '1';
        // common trick: use of directional vectors for up, down, left, right
        int[] directX = {-1, 1, 0, 0};
        int[] directY = {0, 0, -1, 1};
        for (int i = 0; i < directX.length; i++) {
            if (isValid(board, x + directX[i], y + directY[i])) {
                dfs(board, x + directX[i], y + directY[i]);
            }
        }
    }

    private boolean isValid(char[][] board, int x, int y) {
        return x >= 0 && x < board.length && y >= 0 && y < board[0].length && board[x][y] == 'O';
    }
}
複製代碼
相關文章
相關標籤/搜索