原题地址: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. python 全栈开发,Day29(昨日作业讲解,模块搜索路径,编译python文件,包以及包的import和from,软件开发规范)

    一.昨日作业讲解 先来回顾一下昨日的内容 1.os模块 和操作系统交互 工作目录 文件夹 文件 操作系统命令 路径相关的 2.模块 最本质的区别 import会创建一个专属于模块的名字, 所有导入模块 ...

  2. 向集合中添加自定义类型--建议在自定义类型的时候要重写equals方法

    package com.bjpowernode.t01list; import java.util.ArrayList; /* * 向集合中添加自定义类型 */public class TestLis ...

  3. 判断上学和放假的demo

    var today = new Date(); var xq = today.getDay(); var Now = today.getHours(); if (xq >= 1 &&am ...

  4. stl 常用代码

    CString类型的replace ; while((pos = it->m_strFile.find(_T("%UC_INSTALL_ROOT%\\"), pos)) != ...

  5. 观察者模式(Observer Pattern)

    一.概述在软件设计工作中会存在对象之间的依赖关系,当某一对象发生变化时,所有依赖它的对象都需要得到通知.如果设计的不好,很容易造成对象之间的耦合度太高,难以应对变化.使用观察者模式可以降低对象之间的依 ...

  6. [转] web无插件播放RTSP摄像机方案,拒绝插件,拥抱H5!

    需求 问题:有没有flash播放RTSP的播放器?H5能不能支持RTSP播放? 答案:没见过,以后估计也不会有: 问题:可以自己做浏览器插件播放RTSP吗? 答案:可以的,chrome做ppapi插件 ...

  7. maven在windows下的安装

    1.下载 2.解压 3.修改配置环境 4.验证 5.使用mvn help:system就可以看到下载到本地仓库的文件 6.全局settings 7.建议在m2下拷贝一份属于个人的配置settings

  8. 047 大数据下的java client连接JDBC

    1.前提 启动hiveserver2服务 url,username,password. 2.官网 3.程序 4.结果 emp的第一列与第二列 5.源程序 package com.cj.it.hiveU ...

  9. POJ 1375 Intervals 光源投影【平面几何】

    <题目链接> <转载于> 题目大意: 给一个光源点s,给一些圆,源点和s相切会形成阴影,求每一段阴影在横轴上的区间. 解题分析: 这道其实不需要点与圆切线的板子来求解,完全可以 ...

  10. LoRaWAN 1.1 网络协议规范 - 4 MAC 帧格式 Part II

    LoRaWAN 1.1 网络协议规范 LoRaWAN 1.1 版本封稿很久了也没有完整啃过一遍,最近边啃边翻译,趁着这个机会把它码下来. 如果觉得哪里有问题,欢迎留言斧正. 翻译不易,转载请申明出处和 ...