题目:

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的更多相关文章

  1. 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). ...

  2. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  3. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  6. 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~ ...

  7. 【Minimum Window】cpp

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  8. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  9. 【Combination Sum 】cpp

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

随机推荐

  1. [转]超全!iOS 面试题汇总

    转自:http://www.cocoachina.com/programmer/20151019/13746.html 1. Object-c的类可以多重继承么?可以实现多个接口么?Category是 ...

  2. pat乙级1067

    1.用cin输入数据后,再用getline 输入,还是会输入cin已经输入的数据,即cin和getline互相独立. 2.题目中没有说尝试的密码不包含空格,因此不能用cin,而用getline. #i ...

  3. VMware安装win7系统

    1.创建一个虚拟机 2.配置iso映射文件   3.设置boot设置第一启动为cd   4.快速分区后重启电脑,然后选择[A]安装win7.  重启电脑后安装win7系统   搞定...  

  4. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  5. C++各大有名科学计算库(转)

    在 C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功能要好过设计更多的语法的言论.现实中,C++的库门类繁多,解决的问题也是极其广泛,库从轻量级 ...

  6. flash + php对称密钥加密的交互

    这几天研究了下php和flash中的对称密钥加密的交互问题,经过研究以后决定,在项目中使用aes加密.问题也就来了,在flash中的加密数据如何与php的amf进行数据交互,最终决定使用base64编 ...

  7. 初学HBase的几个问题

    转自 http://itindex.net/detail/50571-hbase-%E9%97%AE%E9%A2%98 本文主要针对对HBase不了解的人.主要想基于个人的理解回答以下几个问题: 什么 ...

  8. python_30_购物车复习

    prodcut_list=[ ('Iphone', 5800), ('Mac Pro', 9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ...

  9. kubernetes-存储卷(十二)

    为了保证数据的持久性,必须保证数据在外部存储在docker容器中,为了实现数据的持久性存储,在宿主机和容器内做映射,可以保证在容器的生命周期结束,数据依旧可以实现持久性存储.但是在k8s中,由于pod ...

  10. kubernetes-配置管理(十一)

    Secret https://kubernetes.io/docs/concepts/configuration/secret/ Secret解决了密码.token.密钥等敏感数据的配置问题,而不需要 ...