博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode] 62. Unique Paths 解题报告
阅读量:3673 次
发布时间:2019-05-21

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

题目链接:https://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.

很基本的一道动态规划题目。可以将问题划分为子问题看待,到终点有多少种走法是由到终点左边方格有多少种走法 + 到终点上边有多少种走法,因此很容易得出状态转移方程为:dp[i][j] = dp[i][j-1] + dp[i-1][j]。其中初始化第一列和第一行都为1,因为只有一种走法。时间复杂度为O(M*N), 空间复杂度为O(M*N)。

代码如下:

class Solution {public:    int uniquePaths(int m, int n) {        if(m <=0 || n <=0) return 0;        vector
> dp(m, vector
(n, 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]; }};

转载地址:http://gilbn.baihongyu.com/

你可能感兴趣的文章
用 node.js 开启一个 http服务,返回文件或信息
查看>>
【git】warning: adding embedded git repository
查看>>
git warning: LF will be replaced by CRLF in 解决办法
查看>>
CentOS7制作本地yum源
查看>>
参考花书《深度学习》实现一个简易版PCA
查看>>
CSDN Markdown编辑器——文本颜色、大小、字体设计
查看>>
Looper源码分析
查看>>
MessageQueen源码分析
查看>>
Handler源码分析
查看>>
Thread类的使用
查看>>
单元测试
查看>>
操作系统概述
查看>>
Android内存泄漏分析
查看>>
重学JAVA_IO流——File类
查看>>
重构方法_重新组织函数
查看>>
结构化思维
查看>>
日常记录---编译文件后出现$1.class等文件
查看>>
跟我学UDS(ISO14229) ———— 0x22(ReadDataByIdentifier)
查看>>
跟我学UDS(ISO14229) ———— 0x23(ReadMemoryByAddress)
查看>>
跟我学UDS(ISO14229) ———— 0x2E(WriteDataByIdentifier)
查看>>