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. python作业练习

    #1.猜字游戏 用if分支完成猜数字游戏 -- 先通过键盘输入一个数字算作出题 -- 在键盘输入答案 -- 正确:猜对 | 误差3以内:接近了 | 误差外小值:太小 | 误差外大值:太大 queste ...

  2. jmeter性能测试入门

    1.jmeter介绍2.jmeter变量环境部署3.jmeter目录结构4.jmeter  Gui模式5.jmeter 非Gui模式6.jmeter 录制完显示乱码设置7.jmeter 结果分析 1. ...

  3. 配置json-server

    1.全局安装json-server[可能需要管理员权限] npm i -g json-server 2.创建文件夹jsonerver,初始化package.json文件npm init 3.局部安装j ...

  4. (27)How to stay calm when you know you'll be stressed

    https://www.ted.com/talks/daniel_levitin_how_to_stay_calm_when_you_know_you_ll_be_stressed00:12A few ...

  5. XNA、FNA以及在VS2017中编写XNA

    XNA是早期DX SDK支持的C#游戏开发环境,虽然现游戏开发大多是以Unity3D,Unreal 4为主,但是许多独立游戏开发者,特别是2D游戏,依然使用XNA进行开发, 纠其原因,猜测是C#开源且 ...

  6. hbase单机版安装+phoneix SQL on hbase 单节点安装

    hbase 单机安装部署及phoneix 单机安装 Hbase 下载 (需先配置jdk) https://www.apache.org/dyn/closer.lua/hbase/2.0.1/hbase ...

  7. DOTween的基本用法

    首先声明一点,不要简单的认为 DOTween 只能用在 Transform 组件上完成一些简单的动画,或者是完成一些 UI 动画,DOTween 的用途是很广的,unity中有很多组件都可以使用 DO ...

  8. 缓存为王-varnish

    2.varnish的软件清单 [root@centos69 ~]# rpm -ql varnish /etc/logrotate.d/varnish /etc/rc.d/init.d/varnish ...

  9. TestNG(一)

    1.环境搭建 2.框架结构 3.数据驱动 4.监听器 5.重试逻辑 6.异常测试 7.并发测试

  10. 转 多租户SaaS架构

    当使用Techcello框架开发云端多租户SaaS应用程序时,它继承了经过验证和测试的架构蓝图和工程结构.但开发人员仍然会保留灵活性,自由和控制权,以修改和扩展能力以适应其应用要求.此外,SaaS平台 ...