LeetCode: Unique Paths
来源:互联网 发布:淘宝店保证金能退吗 编辑:程序博客网 时间:2024/06/05 08:14
思路:本题只要写出组合式的计算函数就解决了问题,组合式子有以下关系:
C(n,k) = C(n-1,k) + C(n-1, k-1)
根据这个公式写出计算组合式的函数
后面的问题:
因为m*n个格子一共需要走 m + n - 2步,在其中选择 m -1 向右走,剩下的 n -1 步向下走,就到了目的地,所以结果就是C(m+n -2 , m -1)。
code:
class Solution {public: int combinationValue(int m,int n){ int *temp = new int[m+1], *ret = new int [m+1]; temp[0] = temp[1] = ret[0] = ret[1] = 1; for(int i = 2;i<=m;i++){ for(int j = 1;j<=m;j++){ if(j >= i) ret[j] = j > i? 0 : 1; else ret[j] = temp[j] + temp[j-1]; } ret[0] = 1; for(int j=0;j<=m;j++) temp[j] = ret[j]; } return ret[n]; } int uniquePaths(int m, int n) { int ret = combinationValue(m+n-2,m-1); return ret; }};
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
- Eclipse代码自动补全设置
- cocos2d-x学习笔记-SpriteBatchNode的优化原理
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- TCP/IP协议族——ARP、DNS工作原理及实例详解
- Python学习(1)基本类型
- LeetCode: Unique Paths
- Oracle 10g RAC 添加节点
- selenium使用中遇到的问题
- Fedora 20: How to enable SSH
- 设计模式(3)——观察者模式(Observer Pattern)
- DecimalFormat用法
- 分享一个链接
- 解决StaggeredGridView加载更多滑到顶部的问题
- 【C/C++】merge two array by order