[LeetCode]Unique Paths
来源:互联网 发布:淘宝回收手机是真的吗 编辑:程序博客网 时间:2024/06/07 14:49
今天做的是Unique Paths
62Unique Paths40.3%Medium题目描述是这样的: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?
要求的是给定一个m*n的矩形,每次只能右移或下移一格,求出到达右下角能有多少条路线。
因为我们构造一个m*n的二维数组,对应的result[i][j]的值是从起点到(i+1,j+1)的路线数,因为二维数组是从0起始的,而result[i][j]=result[i-1][j]+result[i]+[j-1],因为对于(i,j)来说,只要有result[i-1][j]条路线到达(i,j+1),再下移一步就可以到达(i+1,j+1);同理,只要到达了(i+1,j),右移一步就可以到达(i+1,j+1),所以将二者加起来就是(i,j)的值。所以最终返回的结果是result[m-1][n-1]。
下面是代码
int uniquePaths(int m, int n) { int results[m][n];for(int i=0;i<m;i++){results[i][0]=1;}for(int j=0;j<n;j++){results[0][j]=1;}for(int i=1;i<m;i++){for(int j=1;j<n;j++){results[i][j]=results[i-1][j]+results[i][j-1];}}return results[m-1][n-1];}
阅读全文
0 0
- leetcode Unique Paths && Unique Paths
- Leetcode-Unique Paths&Unique PathsII
- [LeetCode] Unique Paths and Unique Paths II
- leetcode--Unique Paths && Unique Paths ii
- leetcode--Unique Paths && Unique Paths ii
- 【Leetcode】【python】Unique Paths/Unique Paths II
- LeetCode: Unique Paths
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths
- LeetCode : Unique Paths II
- leetcode 9: Unique Paths
- [LeetCode] Unique Paths
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths
- [LeetCode]Unique Paths II
- [LeetCode] Unique Paths
- 一天一条shell知识------------反引号的应用``
- solr安装和基本应用
- 【Angular】——BootStrap+Swiper实现手机端滑动窗口
- Shell数组
- 队列的链式存储相关操作
- [LeetCode]Unique Paths
- 前端构建工具gulpjs的使用介绍及技巧
- POJ-3041
- angular ui-router 路由默认跳转语句$urlRouterProvider.otherwise(‘路径');与<a>共用时存在的问题
- 自定义文件系统类加载器
- 打印任一年日历
- 搜索专题 O
- Java实现贪吃蛇(三)
- 24C02