原题地址: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).

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.

解题思路:这道题和climbing stairs很像,可以用动态规划解决。状态转移方程为dp[i][j]=dp[i-1][j]+dp[i][j-1]。

代码:

class Solution:
# @return an integer
def uniquePaths(self, m, n):
if m == 1 and n == 1:
list = [[1]]
elif m == 1 and n > 1:
list = [[1 for i in range(n)]]
elif m > 1 and n == 1:
list = [[1] for i in range(m)]
else:
list = [[0 for i in range(n)] for i in range(m)]
for i in range(0, n):
list[0][i] = 1
for i in range(0, m):
list[i][0] = 1
for i in range(1, m):
for j in range(1, n):
list[i][j] = list[i-1][j] + list[i][j-1]
return list[m-1][n-1]

[leetcode]Unique Paths @ Python的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

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

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

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

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

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

  7. Leetcode Unique Paths II

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

  8. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  9. 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. 转换类型 totypeString

    type.totypeString(variable)  其中front type is want to turn    after type是你要转换成的类型 //: dsfsf/Literals. ...

  2. 【C++ Primer 第15章】定义派生类拷贝构造函数、赋值运算符

    学习资料 • 派生类的赋值运算符/赋值构造函数也必须处理它的基类成员的赋值 • C++ 基类构造函数带参数的继承方式及派生类的初始化 定义拷贝构造函数 [注意]对派生类进行拷贝构造时,如果想让基类的成 ...

  3. java 使用反射在dto和entity 实体类之间进行转换

    package com.example.demo.utils; import java.lang.reflect.Method; import java.util.List; import com.e ...

  4. mybatis的sqlSessionFactory的加载过程

    使用过SSM的框架的都知道mybatis这个持久层框架,今天小编就来简单说说这个框架的核心工厂类sqlSessionFactory的加载过程,一般的SSM框架我们都会在spring的applicati ...

  5. SQL基本练习

    .sql对大小写不敏感 .sql执行顺序select--from--where--group by--having--order by .SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号) ...

  6. pyqt5 'QWidget' object has no attribute 'setCentralWidget'(转)

    pyqt5 'QWidget' object has no attribute 'setCentralWidget' 2019年02月18日 16:48:28 wardenjohn 阅读数:78   ...

  7. [洛谷P2123]皇后游戏

    很抱歉,这个题我做的解法不是正解,只是恰巧卡了数据 目前数据已经更新,这个题打算过一段时间再去写. 目前在学习DP,这个会暂时放一放,很抱歉 这个题是一个国王游戏的变形(国王游戏就把我虐了qwq) 题 ...

  8. python新手总结(二)

    random模块 随机小数 random uniform 随机整数 randint randrange 随机抽取 choice sample 打乱顺序 shuffle random.random() ...

  9. 水晶报表Win10(64bit)VS2013安装成功

    windows 10 64 VS2013安装 CR For VS 13_0_18 安装过程没有报错 安装成功http://downloads.businessobjects.com/akdlm/cr4 ...

  10. Playmaker全面实践教程之playMaker编辑器

    Playmaker全面实践教程之playMaker编辑器 playMaker编辑器 playMaker编辑器是制作状态机的主要视图,如图1-23所示.只有熟悉此视图,读者才能更加快捷的使用Playma ...