import java.util.Arrays;

/**
* Source : https://oj.leetcode.com/problems/unique-paths/
*
*
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
*
* The robot can only move either down or right at any point in time. The robot is trying to reach
* the bottom-right corner of the grid (marked 'Finish' in the diagram below).
*
*
* start  
* +---------+----+----+----+----+----+
* |----| | | | | | |
* |----| | | | | | |
* +----------------------------------+
* | | | | | | | |
* | | | | | | | |
* +----------------------------------+
* | | | | | | |----|
* | | | | | | |----|
* +----+----+----+----+----+---------+
* finish
*
*
* How many possible unique paths are there?
*
* Above is a 3 x 7 grid. How many possible unique paths are there?
*
* Note: m and n will be at most 100.
*
*/
public class UniquePaths { /**
* 相当于走迷宫,不过只可以向前或者向下,两个方向
*
* 使用深度优先,递归查找
*
* @param x
* @param y
* @param m
* @param n
* @param result
* @return
*/
public int walk (int x, int y, int m, int n, int result) {
if (x == n - 1 && y == m - 1) {
return result + 1;
}
if (x < n - 1) {
result = walk(x+1, y, m, n, result);
}
if (y < m - 1) {
result = walk(x, y+1, m, n, result);
}
return result;
} public int finAllUniquePaths (int m, int n) {
if (m <= 0 || n <= 0) {
return 0;
}
return walk(0, 0, m, n, 0);
} /**
* 使用动态规划
* dp[i][j] = dp[i][j-1] + do[i-1][j]
*
* 边界条件
* 如果i = 0或者 j = 0,dp[0][j] = 1;
*
* @param m
* @param n
*/
public int finAllUniquePathsByDP (int m, int n) {
int[][] maze = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
maze[i][j] = 1;
} else {
maze[i][j] = maze[i-1][j] + maze[i][j-1];
}
}
}
return maze[m-1][n-1];
} /**
* 动态规划,使用滚动数组
*
* @param m
* @param n
* @return
*/
public int finAllUniquePathsByDPAndScrollArray (int m, int n) {
int[] scrollArray = new int[n];
Arrays.fill(scrollArray, 1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
scrollArray[j] += scrollArray[j-1];
}
}
return scrollArray[n -1];
} public static void main(String[] args) {
UniquePaths uniquePaths = new UniquePaths();
System.out.println(uniquePaths.finAllUniquePaths(1,1));
System.out.println(uniquePaths.finAllUniquePaths(1,2));
System.out.println(uniquePaths.finAllUniquePaths(1,3)); System.out.println(uniquePaths.finAllUniquePaths(2,2));
System.out.println(uniquePaths.finAllUniquePaths(2,3));
System.out.println(uniquePaths.finAllUniquePaths(3,3)); System.out.println("======dp======="); System.out.println(uniquePaths.finAllUniquePathsByDP(1,1));
System.out.println(uniquePaths.finAllUniquePathsByDP(1,2));
System.out.println(uniquePaths.finAllUniquePathsByDP(1,3)); System.out.println(uniquePaths.finAllUniquePathsByDP(2,2));
System.out.println(uniquePaths.finAllUniquePathsByDP(2,3));
System.out.println(uniquePaths.finAllUniquePathsByDP(3,3)); System.out.println("======dp use scroll array=======");
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(2,2));
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(2,3));
System.out.println(uniquePaths.finAllUniquePathsByDPAndScrollArray(3,3)); }
}

leetcode — unique-paths的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. Leetcode Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. C++矩阵加速经典题目:Warcraft III 守望者的烦恼 [vijos 1067]

    Warcraft III 守望者的烦恼 背景 守望者-warden,长期在暗夜精灵的的首都艾萨琳内担任视察监狱的任务,监狱是成长条行的,守望者warden拥有一个技能名叫"闪烁", ...

  2. web安全系列1:入侵的途径

    大家好,接下来的很长一段时间我都会介绍和web安全有关的知识,欢迎大家关注和转发. 话不多说,我们首先来看看今天的主题----入侵的途径.当然,今天介绍的都是针对web网站的常用手法和技巧. 不可否认 ...

  3. intent和手势探测

    一.三种启动方法 setComponent ComponentName comp = new ComponentName( this, SecondActivity.class); Intent in ...

  4. PID control

    |—平滑化算法 |—PID控制—|—P控制器编程 |—PD控制编程 |—PID控制编程 |—参数优化 |—实验P.PD.PID对减小系统误差的作用 这里讨论怎么将路径转变成行动指令(生成平滑的路径), ...

  5. 【repost】Python正则表达式

    星光海豚   python正则表达式详解 正则表达式是一个很强大的字符串处理工具,几乎任何关于字符串的操作都可以使用正则表达式来完成,作为一个爬虫工作者,每天和字符串打交道,正则表达式更是不可或缺的技 ...

  6. 2019/3/7 Java学习之多线程(基础)

    Java学习之多线程 讲到线程,就必须要懂得进程,进程是相当于一个程序的开始到结束,而线程是依赖于进程的,没有进程,就没有线程.线程也分主线程和子线程,当在主线程开启子线程时,主线程结束,而子线程还可 ...

  7. mysql的ACID的理解

    这是在网上copy下来的ACID的概念,可以直接跳过看后面: 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节.事务执行过程中出错,会回滚到事务开 ...

  8. zabbix环境搭建

    zabbix介绍 zabbix是一个开源的监控软件集成了nagos和cat的优势 而且有很多自带的插件可以使用,而且还有api接口供我们使用 zabbix还支持自定义监控项 初始环境- centos ...

  9. Centos6.5---samba文件共享服务配置(二)

    Linux-----samba服务配置(二) 需求: 某公司销售部门提出一个文件共享需求,要求部门共享目录有三个,第一个共享目录所有销售部门人员都具有可读可写权限:第二个共享目录所有销售人员只读权限, ...

  10. DelphiXE10.2.3——跨平台生成验证码图片

    $("#img-code").bind( 'click', function () { $(this).attr('src','VerifyCode?t='+Math.random ...