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

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

【题目】

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 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 = 2Output: 3Explanation:From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:1. Right -> Right -> Down2. Right -> Down -> Right3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3Output: 28

【题意】

从格子的左上角走到格子的右下角一共有多少种走法。

【解答】

使用二维矩阵来存储路径数目。在每一个点,要么从上方走来,要么从左边走来,所以matrix[i][j] = matrix[i-1][j] + matrix[i][j-1]。最上面一行以及最左边一列的走法数目都为1,如此更新matrix即可。

递归的话一定会超时。

时间O(m*n) 空间O(m*n)。

class Solution {public:    int uniquePaths(int m, int n) {        long long matrix[n][m] = {
0}; for(int i=0; i

 

转载于:https://www.cnblogs.com/xxinn/p/10312192.html

你可能感兴趣的文章
python生成.exe文件
查看>>
STM32,你了解多少?(转载)
查看>>
用anaconda保证64位和32位的python共存
查看>>
cPanel设置自定义404错误页
查看>>
16.垃圾最小化
查看>>
ROS time stamp and sync
查看>>
将 Shiro 作为应用的权限基础 三:基于注解实现的授权认证过程
查看>>
遍历聚合对象中的元素——迭代器模式(四)
查看>>
Ehab and subtraction(思维题)
查看>>
Codeforces Round 56-C. Mishka and the Last Exam(思维+贪心)
查看>>
统计汉字
查看>>
使用JavaScript重定向URL参数
查看>>
Tomcat系列(5)——Tomcat配置详细部分
查看>>
python生成器
查看>>
Mybatis 面试题
查看>>
Oracle入门《Oracle介绍》第一章1-4 Oracle 用户管理
查看>>
MPEG文件中什么是GOP
查看>>
贪心算法
查看>>
网络侦查与网络扫描
查看>>
循环链表
查看>>