【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 ...
随机推荐
- 将vim配置成一个轻量的IDE开发工具
1.插件管理工具 2.安装插件 3.配置.vimrc 1.插件管理工具 vim的插件有很多,为了后面方便添加新的插件,我们需要一个插件管理工具来帮我们管理安装的插件,这里使用的是vim-pathoge ...
- 转:WPF中ListBox的创建和多种绑定用法
先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox ...
- 在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
父窗口: ///<summary> ///弹出窗口 ///</summary> ///<param name="sender"></pa ...
- uvm_dpi——DPI在UVM中的实现(一)
文件: src/dpi/uvm_dpi.svh 类: 无 SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...
- cms-最近更新
在这一讲中有几个很重要的地方需要注意: 1.在查询帖子的时候需要把帖子类型id带到帖子类型表中把类型查询出来 2.在字帖子查询语句中用limt限制查询那个阶段的帖子 3.在界面显示的时候需要用到字符串 ...
- ubuntu常见错误
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决 ubuntu常见错误--Could not get lock /var/lib/dpkg/loc ...
- HDU 1114 Piggy-Bank 猪仔储钱罐(完全背包)
题意: 给定一个存钱罐中要存硬币,知道空罐的重量和欲装满的重量,是否能装入?若能,打印最小价值.(注:能装的硬币重量一定刚刚好,里面的总价值要达到最小) 输入: 包含了T个测试例子,在第一行给出.接下 ...
- Win 10 Google 云端硬盘 网页证书问题导致无法登录解决办法
操作方法 按照图示,将以下3项勾去并确定. 效果 设置以后,可成功访问:
- Android(java)学习笔记152:采用get请求提交数据到服务器(qq登录案例)
1.GET请求: 组拼url的路径,把提交的数据拼装url的后面,提交给服务器. 缺点:(1)安全性(Android下提交数据组拼隐藏在代码中,不存在安全问题) (2)长度有限不能超过4K(h ...
- Centos 编译安装bind错误
[root@linux-node1 tmp]# tar xf bind-9.11.1-P1.tar.gz [root@linux-node1 tmp]# cd bind-9.11.1-P1 [root ...