Middle-题目21:62. Unique Paths

原创 2016年05月31日 15:44:07

题目原文:
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?
题目大意:
一个机器人从一个m*n的网格的左上角开始走,每次只能向下或向右走一步,问有几种走到右下角的不同走法?
题目分析:
使用dp,设dp[i][j]是从(0,0)点走到(i,j)点的不同走法数,则dp[i][j]=dp[i-1][j]+dp[i][j-1],初始化第一行和第一列为1,(因为只有一行或一列的时候,是唯一走法)
源码:(language:java)

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 i=0;i<n;i++)
            dp[0][i]=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];
    }
}

成绩:
1ms,beats 5.98%,众数1ms,77.94%
cmershen的碎碎念:
小学学过奥数的人会对“数最短路线”的算法很熟悉,其实本质是一样的。可是那时候不懂编程,更不懂dp,就知道在网格上累加。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum

算法系列博客之Dynamic Programming 本篇博客将运用动态规划的思想来解决leetcode上264号问题这三个题目的共同之处在于均是二维矩阵上的规划问题问题描述:62 Unique Pa...
  • Quiteen
  • Quiteen
  • 2017年06月11日 20:40
  • 125

Leetcode62 Unique Paths

Unique Paths A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diag...

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

[LeetCode]62.Unique Paths

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

leetcode解题之62&63. Unique Paths ||64. Minimum Path Sum java版(路径(最短)可达)

leetcode解题之62&63. Unique Paths || java版(路径可达) ,63. Unique Paths II ,62. Unique Paths 。64. Minimum Pa...

62. Unique Paths -Medium

Question A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram ...

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the ...
  • makuiyu
  • makuiyu
  • 2015年03月15日 23:32
  • 498

算法设计Week12 LeetCode Algorithms Problem #62 Unique Paths

题目描述: A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram bel...

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

[Leetcode刷题总结系列][Dynamic Programming]62. Unique Paths

Leetcode Dynamic Programming 62. Unique Paths A robot is located at the top-left corner of a m*n gr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目21:62. Unique Paths
举报原因:
原因补充:

(最多只允许输入30个字)