4 - BFS & Topological Algorithm

433. Number of Islands

https://www.lintcode.com/problem/number-of-islands/description?_from=ladder&&fromId=1

BFS:

    class Coordinate {
        int x;
        int y;
        Coordinate(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    
    public int numIslands(boolean[][] grid) {
        // write your code here
        if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
        int row = grid.length;
        int col = grid[0].length;
        int count = 0;
        for(int i = 0; i < row; i++) {
            for(int j = 0; j < col; j++) {
                if(grid[i][j]) {
                    checkBFS(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    
    public void checkBFS(boolean[][] grid, int x, int y) {
        int[] directionX = {0, 1, -1, 0};
        int[] directionY = {1, 0, 0, -1};
        Queue<Coordinate> queue = new LinkedList<>();
        queue.offer(new Coordinate(x, y));
        grid[x][y] = false;
        while(!queue.isEmpty()) {
            Coordinate coor = queue.poll();
            for(int i = 0; i < 4; i++) {
                Coordinate adj = new Coordinate(
                    coor.x + directionX[i],
                    coor.y + directionY[i]
                );
                if(!inBound(grid, adj.x, adj.y)) {
                    continue;
                }
                if(grid[adj.x][adj.y]) {
                    grid[adj.x][adj.y] = false;
                    queue.offer(adj);
                }
            }
        }
    }
    
    public boolean inBound(boolean[][] grid, int x, int y) {
        int row = grid.length;
        int col = grid[0].length;
        if(x >= 0 && x < row && y >= 0 && y < col) {
            return true;
        }
        return false;
    }

69. Binary Tree Level Order Traversal

把每一层元素都压入queue中

https://www.lintcode.com/problem/binary-tree-level-order-traversal/description?_from=ladder&&fromId=1

public class Solution {
    /**
     * @param root: A Tree
     * @return: Level order a list of lists of integer
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        // write your code here
        List<List<Integer>> result = new LinkedList<>();
        if(root == null) return result;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> curr = new LinkedList<>();
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                curr.add(node.val);
                if(node.left != null) {
                    queue.offer(node.left);
                }
                if(node.right != null) {
                    queue.offer(node.right);
                }
            }
            result.add(curr);
        }
        return result;
    }
}

猜你喜欢

转载自www.cnblogs.com/jenna/p/10781518.html
今日推荐