[swift] LeetCode 62. Unique Paths

栏目: Swift · 发布时间: 7年前

内容简介: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 dia

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.

func uniquePaths(_ m: Int, _ n: Int) -> Int {
    pathMatrix = Array(repeating: Array(repeating: 0, count: n+1), count: m+1)
    return pathMatrix(m, n)
}
 
private var pathMatrix: [[Int]]!
private func pathMatrix(_ m: Int, _ n: Int) -> Int {
    if m <= 0 || n <= 0 { return 0 }
    if m < 2 || n < 2 { pathMatrix[m][n] = 1; return 1 }
 
    if pathMatrix[m-1][n] == 0 {
        pathMatrix[m-1][n] = pathMatrix(m-1, n)
    }
    if pathMatrix[m][n-1] == 0 {
        pathMatrix[m][n-1] = pathMatrix(m, n-1)
    }
    return pathMatrix[m-1][n] + pathMatrix[m][n-1]
}
[swift] LeetCode 62. Unique Paths [swift] LeetCode 62. Unique Paths

(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+云数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站,所以我又把打赏链接放上来啦~所有打赏金额都会被记账并投入博客维护中,感谢厚爱,多多关照~)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

轻资产创业

轻资产创业

蔡余杰 / 广东人民出版社 / 2017-11 / 45.00元

在互联网时代,资金和资源已经不是制约创业的关键因素。如今即便没有充足的资金和资产做后盾,创业梦依旧可以成为现实。相信轻资产创业模式能够帮助众多经营管理者和创业者实现管理与创业的梦想。 轻资产创业存在误区,如何跨过? 如何巧用四大模式让自媒体创业落地? 如何用一个点子引发创意型创业? 如何利用电商平台实现流量为王的营销型创业? 如何巧用知识节点做好知识产型创业? ......一起来看看 《轻资产创业》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具