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. Codeforces 837 简要题解

    文章目录 A题 B题 C题 D题 E题 F题 G题 传送门 并没有找到难度评级但感觉是div3div3div3场. A题 题意:一个单词的价值是里面大写字母的个数,一篇文章的价值是里面所有单词的价值的 ...

  2. Nginx服务器中配置非80端口的端口转发方法详解

    这篇文章主要介绍了Nginx服务器中配置非80端口的端口转发方法详解,文中使用到了Nginx中的proxy_pass配置项,需要的朋友可以参考下 nginx可以很方便的配置成反向代理服务器: 1 2 ...

  3. 我的Unity学习笔记之——Unity中从网站下载ab资源+下载存储一条龙

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Net ...

  4. 编程感悟-建立好代码sop

    1.最近学django和python,发现很多的函数记不住,这时候我百度了一下,发现记不住是程序员的正常情况, 这下心安理得多了,记好笔记和咱的sop吧,会很快. 社会是不是也这样,好多东西也记不住, ...

  5. Waiting for table metadata lock

    出现下图这个现象之前是在一张事务操作频繁地表上,执行了truncate操作. mysql.sock@(none)> select user,host,db,command,time,state, ...

  6. 与http协作的web服务器--代理、网关、隧道

    一台服务器可以搭建多个web站点 代理: 接受客户端发送的请求,转发给其他服务器,然后接受服务器的返回结果(响应)再返回给客户端.每次经过代理服务器,就会追加写入via首部信息. 按两种基准分类.一种 ...

  7. idea 新建项目上传至git(coding)

    一.新建项目 1.改为git版本 2.出现如下框 选择Git 3.新建一个.gitignore file (Git) 4.勾掉一些不需要的 5.出现如下框 5.1.如果不知道.gitignore fi ...

  8. 第十二章 FTP服务器安装与配置

    习题 1.简述FTP的连接模式. FTP的连接模式有PORT和PASV两种,其中PORT模式是主动模式,PASV是被动模式, 这里所说的主动和被动都是相对于服务器而言的.如果是主动模式,数据端口为20 ...

  9. Linux上搭建Hadoop集群

    本文将为初学者的搭建简单的伪分布式集群,将搭建一台虚拟机,用于学习Hadoop 工具:vm虚拟机,centOS7,jdk-8,Hadoop2.7,xftp,xshell 用户:在虚拟机中创建一个had ...

  10. Linux下使用openVPN连接到某个内网

    推荐一个网站(比较全的介绍关于openvpn的客户端与服务端的配置) 点击我 此处我介绍我配置openvpn客户端连接的坑 我的机器为kali linux apt-get install openvp ...