题目:

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).

How many possible unique paths are there?


Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

分析:

简单说就是给一个m*n的网格,从左上角走到右下角,只能往下前进一格或往右前进一格,共有多少种走法。

到(m,n)格的路径数等于到(m-1,n)的路径数加上(m,n-1)的路径数,根据这个我们可以通过递推求得结果,机器人初始的位置路径数等于1,注意边界条件的判定,也可以将二维数组多开辟一行一列,用来跳过边界条件的处理,还可以先将第一行和第一列都初始化为1,再进行递推求解。

此外我们还可以通过递归求解此问题,即uniquePaths[m][n] = uniquePaths(m-1, n) + uniquePaths(m, n-1),只不过我们不是从起点求到终点,而是最先求右下角的路径数通过递归求解,同样也要注意递归终止条件。

程序:

C++

//Solution 1
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> res(m+, vector<int>(n+, ));
for(int i = ; i < m+; ++i){
for(int j = ; j < n+; ++j){
if(i == && j == )
res[][] = ;
else
res[i][j] = res[i-][j] + res[i][j-];
}
}
return res[m][n];
}
};
//Solution 2
class Solution {
public:
int uniquePaths(int m, int n) {
if(m < || n < ) return ;
if(m == && n == ) return ;
if(res[m][n] > ) return res[m][n]; res[m][n] = uniquePaths(m-, n) + uniquePaths(m, n-);
return res[m][n];
}
private:
int res[][]={};
};

Java

class Solution {
public int uniquePaths(int m, int n) {
int[][] res = new int[m+1][n+1];
for(int i = 1; i < m+1; ++i)
for(int j = 1; j < n+1; ++j){
if(i == 1 && j == 1){
res[1][1] = 1;
}
else{
res[i][j] = res[i-1][j] + res[i][j-1];
}
}
return res[m][n];
}
}
class Solution {
private int[][] res = new int[101][101];
public int uniquePaths(int m, int n) {
//int[][] res = new int[m+1][n+1];
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
if(res[m][n] > 0) return res[m][n]; res[m][n] = uniquePaths(m-1, n) + uniquePaths(m, n-1);
return res[m][n];
}
}

LeetCode 62. Unique Paths不同路径 (C++/Java)的更多相关文章

  1. [LeetCode] 62. Unique Paths 唯一路径

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

  2. [leetcode]62. Unique Paths 不同路径

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

  3. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  4. LeetCode 62. Unique Paths(所有不同的路径)

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

  5. [LeetCode] 62. Unique Paths 不同的路径

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

  6. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  7. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  8. 62. Unique Paths不同路径

    网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...

  9. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

随机推荐

  1. MongoDB学习笔记(四、MongoDB安全管理)

    目录: mongoDB角色 mongoDB初始化账号 mongoDB安全认证 其它常用的命令 mongoDB角色: mongoDB初始化账号: 1.启动mongoDB ./mongod -f mong ...

  2. python 1 默写用递归实现无限极分类 2 默写用树实现无限极分类

    data=[ {"cat_id":3,"name":"沙河","parent_id":1}, {"cat_id ...

  3. 关闭Chrome浏览器的广告

    生活没有绝对的对与错:代码就不一样了,错了就编译不过,也正是因为这样,编程的人思维有时也会陷入一种狭隘中,这就是把工作和生活没有分开.Win10 右下角的广告就像程序调试中的"警告" ...

  4. 导出HTML5 Canvas图片并上传服务器功能

    这篇文章主要介绍了导出HTML5 Canvas图片并上传服务器功能,文中通过实例代码给大家介绍了HTML5 Canvas转化成图片后上传服务器,代码简单易懂非常不错,具有一定的参考借鉴价值,需要的朋友 ...

  5. vue动画实现方式

    vue动画实现方式 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...

  6. 2、Hibernate持久化编写

    一.对于hibernate中的PO编写规则: 1. 必须提供一个无参数的public构造方法   2. 所有属性要private ,对外提供public 的get/set方法   3. 在PO类必须提 ...

  7. Shell(1)---变量

    Shell(1)---变量 初衷:学习shell的目的很简单,自己经常在linux服务器上做各种操作,而且基本上是一些相同的命令操作,所以就想通过shell脚本来启动就行,能够节省一定的开发时间,提高 ...

  8. 为了能早点买房,我用 Python 预测房价走势!

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python高校 PS:如有需要Python学习资料的小伙伴可以加 ...

  9. JQuery Easy UI 1.7官网最新版附1.7API

    最新的Easy UI 1.7包括1.7的API已上传至百度云盘,有需要的,请自行下载!!! 使用示例: 注:红色加粗的必须引入!!! <!DOCTYPE html> <html> ...

  10. Jquery补充及插件

    此篇为jQuery补充的一些知识点,详细资料请看另一篇博客,地址:https://www.cnblogs.com/chenyanbin/p/10454503.html 一.jQuery中提供的两个函数 ...