Lintcode: Unique Paths
C++
dp
递推式:dp[i][j] = dp[i-1][j] + dp[i][j-1]
初值:dp[i][j] = 1,i=0 or j=0
空间优化:省掉一维
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
// wirte your code here
vector<vector<int> > dp(m,vector<int>(n));
for (int i = ; i < m ; ++i) {
for (int j = ; j < n; ++j) {
if ( i == ) {
dp[i][j] = ;
} else if ( j == ) {
dp[i][j] = ;
} else {
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
}
return dp[m - ][n - ];
}
};
空间优化
class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
// wirte your code here
// vector<vector<int> > dp(m,vector<int>(n));
vector<int> dp(n);
for (int i = ; i < m ; ++i) {
for (int j = ; j < n; ++j) {
if ( i == ) {
dp[j] = ;
} else if ( j == ) {
dp[j] = ;
} else {
dp[j] = dp[j] + dp[j - ];
}
}
}
return dp[n - ];
}
};
Lintcode: Unique Paths的更多相关文章
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II
这题在Unique Paths的基础上增加了一些obstacle的位置,应该说增加的难度不大,但是写的时候对细节的要求多了很多,比如,第一列的初始化会受到之前行的第一列的结果的制约.另外对第一行的初始 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
随机推荐
- 【推荐】腾讯android镜像(做Android开发的得好好利用下这个网站,国内的大公司还是可以滴……)
原文地址:http://android-mirror.bugly.qq.com:8080/include/usage.html ☀ Windows I. Open Android SDK Manage ...
- 【nginx】配置Nginx实现负载均衡
一文中已经提到,企业在解决高并发问题时,一般有两个方向的处理策略,软件.硬件,硬件上添加负载均衡器分发大量请求,软件上可在高并发瓶颈处:数据库+web服务器两处添加解决方案,其中web服务器前面一层最 ...
- 完全定制UITabBarViewController
完全定制UITabBarViewController 效果 源码 https://github.com/YouXianMing/iOS-Project-Examples 中的 TotalCusto ...
- Lua游戏开发参考资料收集
table {border-collapse:collapse;} List of game engines that use lua: 2D Agen (Lua; Windows) Blitwiza ...
- 开源项目PullToRefresh详解(一)——PullToRefreshListView
开源项地址:https://github.com/chrisbanes/Android-PullToRefresh 下拉刷新这个功能我们都比较常见了,今天介绍的就是这个功能的实现.我将按照这个开 ...
- Eclipse中输入变量会自动补全上屏的解决方法
我自己在启动Eclipse代码补全后输入感觉确实爽多了,但是每次输入变量后一按下空格,编译器会自己帮你写一个很蛋疼的名字,比如你输入了:String mStr后按下空格,它就变成了mString,十分 ...
- Easyui layout设置满屏效果
html文件: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- SpiderMonkey js引擎的静态编译与使用, SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用 2017年10月02日 02:51:22 yaolixing01 阅读数:536 原文出处: http://yaolixing.oltag.co ...
- 迪米特法则(Law Of Demeter)
定义:一个对象应该对其他对象保持最少的了解. 问题由来:类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案:尽量降低类与类之间的耦合. 自从我们接触编程开始,就 ...
- dubbo源码解析-spi(二)
前言 上一篇简单的介绍了spi的基本一些概念,在末尾也提到了,dubbo对jdk的spi进行了一些改进,具体改进了什么,来看看文档的描述 JDK 标准的 SPI 会一次性实例化扩展点所有实现,如果有扩 ...