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. nginx上传文件时 nginx 413 Request Entity Too Large 错误

    产生原因: 上传文件的大小超出了 Nginx 允许的最大值,默认是1M: 解决方法: 修改Nginx的配置文件(一般是:nginx/nginx.conf),在 http{} 段中增大nginx上传文件 ...

  2. web安全系列4:google语法

    这是web安全的第四篇,欢迎翻看前面几篇. 前面我们介绍了一些和HTTP有关知识,那么一个疑问就是黑客要做的第一件是什么?其实很简单,确定一个目标,然后搜集信息. 这很容易理解,我们无论做什么都得先有 ...

  3. Hibernate框架:CRM练习--保存客户

    crm:customer ralation manager 客户关系管理系统 一.准备 1.创建web项目 2.导包 最终为: 3.引入静态页面 将文件复制放入项目的WebContent目录下面: 4 ...

  4. C#实现简单的RPC

    demo地址:https://pan.baidu.com/s/1PeTdV2V9DF87jZTHdz4CyA 提取码:n2qm 参考地址:https://github.com/neuecc/Magic ...

  5. mybatis框架的注意点

    1.在mapper.xml文件中 resultType:返回结果类型,只能用于单表 paramsType:接收参数类型,基本数据类型可省略 2.给实体类起别名 在mybatisConfig.xml配置 ...

  6. C++ Error C2664:无法将参数 1 从“const char [9]”转换为“LPCWSTR”解决方案

    问题出现 编译平台:VS2013     Windows 出现地方:在使用LoadLibrary( )函数动态链接DLL文件时出现的一个问题 Eg.   在使用 UNICODE字符的工程中,  HIN ...

  7. 【MySql】启动/停止

    一.启动 1.查看启动命令所在目录 macdeMacBook-Pro:~ mac$ ps -ef|grep mysql 2.进入命令目录 macdeMacBook-Pro:~ mac$ cd /usr ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. 用JavaScript制作简单的计算器

    <html > <head> <title>简单计算器</title> <style type="text/css"> ...

  10. noip第26课资料