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 ...
随机推荐
- .net项目中使用Quartz
(1)在web.config中进行相关配置 <configSections> <section name="quartz" type="System.C ...
- IIS 调用Microsoft.Office.Interop.Word.Documents.Open 返回为null
控制面板->管理工具->组件服务->计算机->我的电脑->DCom配置->找到Microsoft Word文档 之后 单击属性打开此应用程序的属性对话框. 2. 单 ...
- Java String练习题及答案
1. 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 /** * 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕 */ publ ...
- Android ormlite like() function is not working
//http://stackoverflow.com/questions/7642161/android-ormlite-like-function-is-not-working try { Quer ...
- VisualStudio:添加现有项时使用添加为链接
这个特性很容易忘记使用(很多人可能还不知道),这里解释一下. 添加为链接是指:将指定的文件作为链接添加到项目中,这个文件在作用上和一般的文件没有区别,这样做的好处是可以多个项目共享一个文件,如:连接字 ...
- 将CAGradientLayer当做mask使用
将CAGradientLayer当做mask使用 效果 源码 https://github.com/YouXianMing/Animations // // CAGradientView.h // M ...
- fabric-ca-server
fabric-ca-server start -b admin:adminpw -d --db.type mysql --db.datasource "root:rootpwd@tcp(17 ...
- 成功让Eclipse更新ADT的方法
[本文转载自]http://blog.csdn.net/yihui8/article/details/8044426 原文:配置android开发环境eclipse获取ADT获取不到 https:// ...
- 从源码角度一步一步来修改PreferenceActivity界面
PreferenceActivity给我们封装好了一个数据存储对象,我们只需要在xml文件中写上控件即可完成简单的设置界面.但是系统提供的设置界面十分的简陋,要想做的好看必须要自己来进行修改 ...
- 连接MSSQL2008 Express
(1)打开Manage Studio. (2)click on the .\sqlexpress, select property, click security, make sure use mi ...