[leetcode]Unique Paths @ Python
原题地址: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的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- 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 ...
- [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 ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 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 ...
随机推荐
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- hdu1698
/*区间更新*/#include <cstdio> #include <algorithm> using namespace std; #define lson l , m , ...
- 主机可以ping通虚拟机,但是虚拟机ping不通主机的方法(转)
https://blog.csdn.net/hskw444273663/article/details/81301470
- ERP合同审核流程处理(二十九)
合同审批流程: 前端的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=" ...
- Python 缓冲区
转自:https://blog.csdn.net/pro_leo/article/details/41786397(有修订) 1.Python缓存作用: a.提高执行效率. b.减少内存负担. 2.首 ...
- 如何在Maven官网下载历史版本
如何在Maven官网下载历史版本 历史版本一般会隔一段时间,便找不到,官网会及时显示的是最新版本.不多说,直接进入. https://archive.apache.org/dist/maven/bin ...
- 001.网络TCP/IP工程知识点
一 互联网概述 计算机网络定义:由自主计算机互连起来的集合体. 计算机网络两大部分:硬件:计算机.通信设备.接口设备和传输介质. 软件:通信协议和应用软件. 广域网拓扑结构通常有:网状拓扑结构和环形拓 ...
- 暴力破解ZIP文件密码
Python 的标准库提供了 ZIP 文件的提取压缩模块 zipfile,现在让我们试着用这个模块,暴力破解出加密的 ZIP 文件!我们可以用 extractall()这个函数抽取文件,密码正确则返回 ...
- TFT LCD显示原理详解
<什么是液晶> 我们一般认为物体有三态:固态.液态.气态,其实这只是针对水而言,有一些有机化和物 还有介于固态和液态中间的状态 就是液晶态,如下图(一): ...
- CF915E Physical Education Lessons 动态开点线段树
题目链接 CF915E Physical Education Lessons 题解 动态开点线段树 代码 /* 动态开点线段树 */ #include<cstdio> #include&l ...