博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode — unique-paths-ii
阅读量:6586 次
发布时间:2019-06-24

本文共 2590 字,大约阅读时间需要 8 分钟。

/** * 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));    }}

转载于:https://www.cnblogs.com/sunshine-2015/p/7643850.html

你可能感兴趣的文章
通过包名获取该包下的所有类
查看>>
【JavaScript学习笔记】画图
查看>>
Linux写时拷贝技术(copy-on-write)
查看>>
opencv视频读取问题
查看>>
java Iterator Fail-fast机制
查看>>
Java堆外内存之五:堆外内存管理类ByteBuffer
查看>>
HTML5 input placeholder 颜色修改
查看>>
TJ/T808 终端通讯协议设计与实现(码农本色)
查看>>
分布式搜索引擎Elasticsearch的查询与过滤
查看>>
SolidEdge 工程图中如何给零件添加纹理或贴图
查看>>
【Java面试题】14 super.getClass()方法调用
查看>>
六种流行的语言---C、C++、python、Java、php、C#比较[转]
查看>>
AP INVOICES IMPORT API(NOT request)
查看>>
怎样面试程序猿
查看>>
Redhat6.5安装DB2 Express-C版本
查看>>
php的http数据传输get/post...
查看>>
【剑指Offer面试题】 九度OJ1368:二叉树中和为某一值的路径
查看>>
checkbox的name与JavaBean的交互时发现的一个现象
查看>>
基于Token的身份验证——JWT(转)
查看>>
Maven(五)之Maven配置阿里云镜像飞快下jar包
查看>>