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

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

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?

clipboard.png

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

难度:medium

题目:

在m * n 的格子左上角放置一个机器人,机器人在任何时候只能向右或向下移动,机器人尝试移动到格子的最右下角。有多少种可能的走法?

思路:动态规划,grid[m][n] = grid[m - 1][n] + grid[m][n - 1]

Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths.

Memory Usage: 23.6 MB, less than 41.57% of Java online submissions for Unique Paths.

class Solution {    public int uniquePaths(int m, int n) {        int[][] grid = new int[m][n];        for (int i = 0; i < n; i++) {            grid[0][i] = 1;        }        for (int i = 0; i < m; i++) {            grid[i][0] = 1;        }                for (int i = 1; i < m; i++) {            for (int j = 1; j < n; j++) {                grid[i][j] = grid[i - 1][j] + grid[i][j - 1];            }        }                return grid[m - 1][n - 1];    }}

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

你可能感兴趣的文章
Unity3D工程源码目录
查看>>
杀死进程命令
查看>>
cookie 和session 的区别详解
查看>>
浮点数网络传输
查看>>
Mongodb对集合(表)和数据的CRUD操作
查看>>
面向对象类的解析
查看>>
tomcat如何修改发布目录
查看>>
CentOS 5.5 使用 EPEL 和 RPMForge 软件库
查看>>
Damien Katz弃Apache CouchDB,继以Couchbase Server
查看>>
Target runtime Apache Tomcat is not defined.错误解决方法
查看>>
某机字长为32位,存储容量为64MB,若按字节编址.它的寻址范围是多少?
查看>>
xml.exist() 实例演示
查看>>
ctrl+shift+c
查看>>
rup
查看>>
yum失败
查看>>
eclipse中默认js编辑器非常慢,尤其在拷贝粘贴代码时的解决方法
查看>>
http Post 请求一网络资源返回字符串
查看>>
window策略设置
查看>>
web 开发之js---js 实现地址栏的表单提交加密编码
查看>>
此类目的是防治序列化Json字符串时的循环引用问题-------最好解决方案
查看>>