【Unique Paths】cpp
题目:
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.
代码:
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
memset(dp, , sizeof(dp));
for ( size_t i = ; i < n; ++i ) dp[][i] = ;
for ( size_t i = ; i < m; ++i ) dp[i][] = ;
for ( size_t i = ; i < m; ++i )
{
for ( size_t j = ; j < n; ++j )
{
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};
tips:
常规dp解法。
=====================================
上面的代码有可以改进的地方:dp[m][n]并不用这些额外空间,只需要两个长度为n的数组即可;一个保存前一行的状态,一个用于遍历当前行的状态,每次滚动更新,可以省去额外空间。沿着上述思路改进了一版代码如下:
class Solution {
public:
int uniquePaths(int m, int n) {
int curr[n], pre[n];
for ( size_t i = ; i<n; ++i ) { pre[i]=; curr[i]=; }
curr[] = ;
for ( size_t i = ; i<m; ++i )
{
for ( size_t j = ; j<n; ++j )
{
curr[j] = curr[j-] + pre[j];
pre[j] = curr[j];
}
curr[] = ;
}
return pre[n-];
}
};
这个代码空间复杂度降到了O(n),但还是可以改进。其实只用一个一维的数组dp就可以了,代码如下。
class Solution {
public:
int uniquePaths(int m, int n) {
int curr[n];
memset(curr, , sizeof(curr));
curr[] = ;
for ( size_t i = ; i < m; ++i )
{
for ( size_t j = ; j < n; ++j )
{
curr[j] = curr[j-] + curr[j];
}
}
return curr[n-];
}
};
这里用到了滚动数组的技巧。有个细节需要注意,外层dp是可以从0行开始,省去了一部分代码。
=====================================
再学一种深搜+缓存(即“备忘录”)解法。
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > cache(m+,vector<int>(n+,));
return Solution::dfs(m, n, cache);
}
static int dfs( int x, int y, vector<vector<int> >& cache )
{
if ( x< || y< ) return ;
if ( x== && y== ) return ;
int left = cache[x-][y]> ? cache[x-][y] : cache[x-][y]=Solution::dfs(x-, y, cache);
int up = cache[x][y-]> ? cache[x][y-] : cache[x][y-]=Solution::dfs(x, y-, cache);
return left+up;
}
};
有点儿类似动态规划的思想:开一个cache数组保存已经深搜遍历过的中间结果,避免重复遍历。
这里有个简化代码的技巧:定义cache的时候多定义一行和一列,这样在深搜的过程中按照dfs中的代码可以省去判断cache下标是否越界的逻辑。
===============================================
第二次过这道题,直接写了一个dp的做法。
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
fill_n(&dp[][], m*n, );
for ( int i=; i<n; ++i ) dp[][i]=;
for ( int i=; i<m; ++i ) dp[i][]=;
for ( int i=; i<m; ++i )
{
for ( int j=; j<n; ++j )
{
dp[i][j] = dp[i][j-] + dp[i-][j];
}
}
return dp[m-][n-];
}
};
【Unique Paths】cpp的更多相关文章
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Minimum Window】cpp
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 【Sudoku Solver】cpp
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
随机推荐
- 真正理解 git fetch, git pull 以及 FETCH_HEAD(转)
转自http://www.cnblogs.com/ToDoToTry/p/4095626.html 真正理解 git fetch, git pull 要讲清楚git fetch,git pull,必须 ...
- 利用jsplumb和碰撞检测自动生成流程图
使用jsplumb构建流程图模型时,有一个需求要求,选项可以从选项表中拖拽到指定容器,并且两个选项要接触到的时候才能连接起来,不接触不能连接.效果图如下 略丑- 因为这里用到了拖拽,拖放功能,所以用到 ...
- [转] python 远程主机强迫关闭了一个现有的连接 socket 超时设置 errno 10054
python socket.error: [Errno 10054] 远程主机强迫关闭了一个现有的连接.问题解决方案: 前几天使用python读取网页.因为对一个网站大量的使用urlopen操作,所以 ...
- 汇编:jmp系列跳转指令总结
助记方法: J:跳转C: 进位位置位N: 否S: 符号位置位o: 溢出位置位Z: 零标志位置位E: 等于P:奇偶位置位A: AboveB: BelowL: Less (Little的比较级)G: Gr ...
- 9 Palindrome_Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个数是否是回文数. public class ...
- 【总结】Oracle sql 中的字符(串)替换与转换
1.REPLACE 语法:REPLACE(char, search_string,replacement_string) 用法:将char中的字符串search_string全部转换为字符串repla ...
- World Wind Java开发之十一——加载热点信息(仿Google Earth)(转)
在GE的图层中有一个照片图层,在浏览时可以看到各地的一些图片,我们称之为热点信息,如下图所示: 再来看下本文的实现效果: 效果是不是很像呢,其实实现这个很简单,参照examples中的Balloons ...
- 黑箱中的 retain 和 release
https://github.com/Draveness/Analyze/blob/master/contents/objc/黑箱中的%20retain%20和%20release.md 写在前面 在 ...
- Using Autorelease Pool Blocks
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAut ...
- Jmeter模拟http请求
一.获取用户信息(GET请求):http://hostname/getuser?userid=1 1.打开jmeter,创建一个线程组,再添加一个http请求Sampler 2.设置域名.路径.请求方 ...