题目: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?

题意:在m*n的网格中,从左下角一步步走到右上角,有多少种可能的走法(每次只能横或竖移动一步)

    在第一象限的话,也就是每次走一步从(0,0)走到(m,n)有多少种走法

思路:考虑这是一个递推的问题,根据DP思想,有递推公式

我的代码比较短,因为memset只能置0或者-1,可以把数组置为-1,然后和取负就是所求结果了。

class Solution {
public:
int uniquePaths(int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int f[m][n];
memset(f, -, sizeof(int) * m * n); //数组全部置-1
for (int i = ; i < m; i++) { //求和
for (int j = ; j < n; j++) {
f[i][j] = f[i - ][j] + f[i][j - ];
}
}
return -f[m - ][n - ]; //取负
}
};

时间复杂度O(m*n),那么可不可以继续优化呢?

上面采用的是二维数组,现在可以用一位数组取代之,则

Fn=Fn-1+Fn;

class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> vec(n, );
for(int i=; i<m; ++i){
for(int j=; j<n; ++j){
vec[j]+=vec[j-];
}
}
return vec[n-];
}
};

LeetCode题解——Unique Path(DP与优化)的更多相关文章

  1. LeetCode 63. Unique Path II(所有不同路径之二)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  3. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. [LeetCode]题解:005-Longest Palindromic Substring优化

    题目来源和题意分析: 详情请看我的博客:http://www.cnblogs.com/chruny/p/4791078.html 题目思路: 我上一篇博客解决这个问题的时间复杂度是最坏情况是(O(n^ ...

  5. 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra

    题目描述 FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个 ...

  6. [LeetCode]题解(python):062 Unique path

    题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x  ...

  7. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

  8. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  9. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

随机推荐

  1. SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文章之一)

    SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文章之一) SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭 ...

  2. QTP数据驱动之读取Excel数据

    这个代码的原理是把Excel的数据当做数据库里的数据一样处理,可以对Excel用select来检索需要的数据,然后把数据以键值对的形式保存到oDict里,方便在用例层来调用 Class oDataDi ...

  3. 数组 寻找最大的第k个数

    int Partion(int asy[],int begin,int end) { int k=begin; int key=asy[end]; for(int i=begin;i<=end; ...

  4. BZOJ 2005 能量采集(容斥原理)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2005 题意:给定n和m,求 思路:本题主要是解决对于给定的t,有多少对(i,j)满足x= ...

  5. Hibernate的一个注释 @Transient

    @Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性. 如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basi ...

  6. 1109. Conference(二分图)

    1109 二分图的模板题 不过这题题意 我纠结了好久 不知道是不是我对二分图不熟悉的原因 这题就是说 有n+m个人参加会议 要在这n+m中进行通话 求最少的连接数 就是每个人都得被连接上 这样求最大匹 ...

  7. JSOI2015 分组赛记

    分组赛结束了,虽然跟我关系不大,但是去了还是学到了不少东西 day1 上午报到,在宾馆遇到大神wzy,orz 好像没有参赛证发了,于是给我发了一个[工作证],233我是工作人员了,高贵冷艳 下午是常中 ...

  8. pinyin4j使用示例

    pinyin4j的主页:http://pinyin4j.sourceforge.net/pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制pinyin4j是一个支持将中文转换到拼 ...

  9. ASP.NET MVC 学习8、Controller中的Detail和Delete方法

    参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and ...

  10. kindeditor编辑器图片水印

    //upload_pic.ashx源码 <%@ webhandler Language="C#" class="edit_html_upload_pic" ...