博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
62.Unique Paths
阅读量:4648 次
发布时间:2019-06-09

本文共 1206 字,大约阅读时间需要 4 分钟。

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?

avatar

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

class Solution:    def uniquePaths(self, m, n):        """        :type m: int        :type n: int        :rtype: int        """        dp = [[0 for i in range(n+2)] for j in range(m+2)]        dp[1][2] = dp[2][1] =1        for i in range(1,m+1):            for j in range(1,n+1):                if (i==1 and j==2) or (i==2 and j==1) or(i==1 and j==1):                    dp[i][j]=1                    continue                dp[i][j] = dp[i-1][j] + dp[i][j-1]        return dp[m][n]

转载于:https://www.cnblogs.com/bernieloveslife/p/9762756.html

你可能感兴趣的文章
HDU 3374 String Problem
查看>>
数据集
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>
Django Rest Framework -解析器
查看>>
ExtJs 分组表格控件----监听
查看>>
Hibernate二级缓存配置
查看>>
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
mechanize (1)
查看>>
FactoryBean
查看>>
Coolite动态加载CheckboxGroup,无法在后台中获取
查看>>
如何在我们项目中利用开源的图表(js chart)
查看>>
nfs服务器工作原理
查看>>
C3P0连接池工具类使用
查看>>
SVN常用命令备注
查看>>
孩子教育
查看>>
解决Cacti监控图像断断续续问题
查看>>