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

本文共 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://sxnea.baihongyu.com/

你可能感兴趣的文章
optimize mysql
查看>>
RPM signature: BAD
查看>>
Windows常用CMD网络命令总结
查看>>
Twitter 宣布抛弃 Mesos,全面转向Kubernetes
查看>>
二叉树的下一个结点(未)
查看>>
我的友情链接
查看>>
LINUX下安装dynamips的方法
查看>>
OpenSSL学习(十六):基础-指令rand
查看>>
Apache+tomcat实现高可用WEB集群
查看>>
centos(linix版本)操作命令
查看>>
KeyMob致力于打造国内领先的移动广告平台
查看>>
Shiro的身份认证(Authentication)
查看>>
构建混合云:配置Azure site to site ***连接(3)
查看>>
springboot-使用assembly进行项目打包
查看>>
自动上线测试已经不潮了,你有跟上DevOps的潮流吗?
查看>>
DEX结构解析(一)
查看>>
图片交替渐变出现的效果实现
查看>>
各大搜索引擎网站提交入口
查看>>
如何修改本机MAC地址
查看>>
Java反射机制
查看>>