Leetcode Notes - brush essay questions

 

200 is . Numbers of Islands
 ***********************
 class Solution {
     // Vector four directions defined first, to facilitate calculation of left and right vertical position of the matrix
    final static int [][]dirs = {{-1, 0},
                                 {1, 0},
                                 {0, -1},
                                 {0, 1}
                                 };
    public int numIslands(char[][] grid) {
        // corner case
        if (grid == null || grid.length == 0 || grid[0].length == 0){
            return 0;
        }
        int count = 0;
        final int rows = grid.length;
        int cols Final = Grid [0] .length;
         // with DFS adjacent traverse all ' 1 ' in a position
         for (int I = 0; I <rows; I ++ )
             for (int J = 0; J <cols; J ++ ) {
                 IF (Grid [I] [J] == ' . 1 ' ) {
                    count++;
                    dfs(grid, i, j, rows, cols);
                }
            }
        return count;
    }
    
    public void dfs(char[][]grid, int x, int y, int rows, int cols){
        if (x < 0 || x >= rows || y < 0 || y >= cols || grid[x][y] == '0'){
            return;
        }
        grid[x][y] = '0';
        for (int []dir : dirs){
            int next_x = dir[0] + x;
            int next_y = dir[1] + y;
            dfs(grid, next_x, next_y, rows, cols);
        }
    }
}
***************************************

700. Search in a Binary Search Tree
    

701. Insert into a BST
***************************************
    if root:
        if target < root and not root.left:
            into(root.left)
        elif target > root and not root.right:
            into(root.right)
        else:
            if target < root:
                root.left = target
            else:
                root.right = target

# 63. The Path of Unique 
    Method 1: dynamic programming problem solving from the first recursive
    int uniquePaths(int m, int n) {
        if (m==0||n==0)
            return 0;
        auto f = vector<vector<int>>(n+1, vector<int>(m+1, 0));
        f[1][1] = 1;
        for (int y = 1; y <= n; y++){
            for (int x = 1; x <= m; x++){
                if (x==1 && y==1){
                    continue;              
                }
                else{
                    f[y][x] = f[y-1][x] + f[y][x-1];  
                }
            }
        }
        return f[n][m];
    
    Method 2: Memory of the recursive solution, time-consuming
    public:
    int uniquePaths(int m, int n) {
        if (m<0 || n<0)
            return 0;
        if (m==1 && n==1)
            return 1;
        if (memo[m][n] > 0)
            return memo[m][n];
        int left_path = uniquePaths(m-1, n);
        int up_path = uniquePaths(m, n-1);
        memo[m][n] = left_path + up_path;
        return memo[m][n];
    }
    private:
        unordered_map<int, unordered_map<int, int>> memo;
    
    Summary: The same basic idea is that if the requirement had to go (m, n) location, i.e., f [m] [n], and left only considered from two directions up
    I.e., F [m] [n-] = F [. 1-m] [n-] + F [m] [n--. 1 ], as the starting point until a termination condition.

98. Validate a BST
    Two ways:
        1 . Recursion. Respectively left subtree and right subtree checked by the following recursion:
            The initial state :( -∞ <root.value < ∞)
            Left subtree: ( -∞ <root.left.value < root.val)
            Left subtree: lower bound value remains constant, changes in cutoff of its parent node (root node it), it is determined whether root.left.val smaller than the root node, check (root.left, low, root. val)

            The right subtree of: (root.val <root.right.value < ∞)
            Right subtree of: holding the same sector, the lower bound value of a parent node changes (its root) value,
                    Root.right.val determine whether or larger than the root node, check (root.right, root.val, high)

        2. In order traversal, in - the Order Traversal


107 . Preorder layer
     1. Queue = RES = []
     2 queue.append (the root).
     3. the while len (Queue) =! 0:
        temp = []
        quesize = len(queue)
        for i in range(quesize):
            node = queue.pop(0)
            if node.left is not None:
                queue.append(node.left)
            if node.right is not None:
                queue.append(node.right)
            temp.append(node.val)
        res.append(temp)
    4. return res

 

Guess you like

Origin www.cnblogs.com/lincs97/p/Leetcode.html