zoukankan      html  css  js  c++  java
  • leetcode — unique-paths-ii

    /**
     * Source : https://oj.leetcode.com/problems/unique-paths-ii/
     *
     *
     * Follow up for "Unique Paths":
     *
     * Now consider if some obstacles are added to the grids. How many unique paths would there be?
     *
     * An obstacle and empty space is marked as 1 and 0 respectively in the grid.
     *
     * For example,
     * There is one obstacle in the middle of a 3x3 grid as illustrated below.
     *
     * [
     *   [0,0,0],
     *   [0,1,0],
     *   [0,0,0]
     * ]
     *
     * The total number of unique paths is 2.
     *
     * Note: m and n will be at most 100.
     *
     */
    public class UniquePath2 {
    
        /**
         * 依然使用动态规划
         * 注意障碍,障碍在边上和中间
         *
         * @param maze
         * @return
         */
        public int finAllUniquePaths (int[][] maze) {
            if (maze.length <= 0 || maze[0].length <= 0) {
                return 0;
            }
            int max = 0;
            for (int i = 0; i < maze.length; i++) {
                for (int j = 0; j < maze[0].length; j++) {
                    if (maze[i][j] == 1) {
                        // 障碍处为0
                        max = maze[i][j] = 0;
                    } else {
                        if (i > 0 && j > 0) {
                            max = maze[i][j] = maze[i-1][j] + maze[i][j-1];
                        } else if (i > 0) {
                            // 第一列不一定是1
                            max = maze[i][j] = maze[i-1][j];
                        } else if (j > 0) {
                            // 第一行不一定是1
                            max = maze[i][j] = maze[i][j-1];
                        } else {
                            // 第一个是1
                            max = maze[i][j] = 1;
                        }
                    }
                }
            }
            return max;
        }
    
        public static void main(String[] args) {
            UniquePath2 uniquePaths = new UniquePath2();
            int[][] arr = new int[][]{
                    {0,1},
                    {0,0}
            };
            int[][] arr1 = new int[][]{
                    {0,1,0},
                    {0,0,0}
            };
            int[][] arr2 = new int[][]{
                    {0,1,0},
                    {0,1,0},
                    {0,0,0}
            };
            int[][] arr3 = new int[][]{
                    {0,0,0},
                    {0,1,0},
                    {0,0,0}
            };
            int[][] arr4 = new int[][]{
                    {0,0,0,0,0,0,0},
                    {0,1,0,0,0,0,0},
                    {0,0,0,0,0,0,0}
            };
            System.out.println(uniquePaths.finAllUniquePaths(arr));
            System.out.println(uniquePaths.finAllUniquePaths(arr1));
            System.out.println(uniquePaths.finAllUniquePaths(arr2));
            System.out.println(uniquePaths.finAllUniquePaths(arr3));
            System.out.println(uniquePaths.finAllUniquePaths(arr4));
        }
    
    }
    
  • 相关阅读:
    用原生PHP做Blog系统-Day01
    PHP做猜数字游戏
    关于html头部引用(meta,link)
    gulp基本入门
    前端构建工具gulpjs的使用介绍及技巧
    $.ajax()方法详解 jquery中的ajax方法
    js string 转 int 注意的问题——parseInt
    经常玩电脑怎么防辐射
    js 禁止重复提交
    jquery 监听回车提交
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7643850.html
Copyright © 2011-2022 走看看