Level:

  Medium

题目描述:

  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

思路分析:

  题目要求求出一个机器人从矩阵的左上角走到矩阵的右下角,一共有多少种走法。可以用动态规划的思想来解决这道题,我们用dp[ i ] [ j ]来表示走到第i行和第j列,共有多少种走法。由题意知,机器人只能向右和向下走,那么状态转移方程是 dp[ i ] [ j ]=dp [i-1] [ j ]+dp[ i ] [ j-1]。注意到矩阵第一行或者第一列某个位置,路径只有一条。(因为起点是左上角,并且只能向右向下移动)

代码:

public class Solution{
public int uniquePaths(int m,int n){
int [][]dp=new int [m][n];
for(int i=0;i<m;i++){
dp[i][0]=1;
}
for(int j=0;j<n;j++){
dp[0][j]=1;
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
return dp[m-1][n-1];
}
}

35.Unique Paths(不同的路径)的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. LeetCode OJ: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 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). ...

  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] unique paths ii 独特路径

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

  7. 063 Unique Paths II 不同路径 II

    这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[  ...

  8. Leetcode63.Unique Paths II不同路径2

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...

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

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

随机推荐

  1. js利用递归与promise 按顺序请求数据

    问题:项目中有一个需求,一个tabBar下面如果没有内容就不让该tabBar显示,当然至于有没有内容,需要我们通过请求的来判断,但是由于请求是异步的,如何让请求按照tabBar的顺序进行? 方案:我们 ...

  2. C# 对象转JSON字符串

    对象转JSON字符串 /// <summary> /// 对象转Json字符串 /// </summary> /// <param name="obj" ...

  3. Sass-注释

    注释对于一名程序员来说,是极其重要,良好的注释能帮助自己或者别人阅读源码.在 Sass 中注释有两种方式,我暂且将其命名为: 1.类似 CSS 的注释方式,使用 ”/* ”开头,结属使用 ”*/ ”2 ...

  4. maven多模块

    https://www.cnblogs.com/lichking2017/p/8996939.html

  5. java下载文件demo

    java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html

  6. mongodb 索引基础

    一 .索引基础:    MongoDB的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的优化技巧.下面是创建索引的命令:    > db.test.ensureIndex({&quo ...

  7. elasticsearch查询与sql对应关系

    must: AND must_not:NOT should:OR

  8. Java调用明华RF读写器DLL文件的方法

    首先jdk必须得是32位的,IDE也必须是32位的(我用的idea,所以为了使用32位的,下载了2018年1月版本的). 明华RF读写器演示文件提供了一份名为mwrf32.dll的动态链接库文件 ja ...

  9. vfs的super block

    super block这个数据结构,乃至super block在磁盘上的位置,是哪里的规定? 没规定,1k偏移只是ext文件系统.但是像fat,它们第0扇区后就是保留扇区,但linux一样要识别它们. ...

  10. java-设计原则

    七大设计原则 单一职责原则: 尽可能的功能细分(类细分,方法细分):如一个类由于某变量而细分方法,该细分方法再细分,需要重构(最好细分类) 接口隔离原则:(C类实现A接口全部方法,而D,B类依赖于A接 ...