LeetCode题解——Unique Path(DP与优化)
题目: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与优化)的更多相关文章
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode]题解:005-Longest Palindromic Substring优化
题目来源和题意分析: 详情请看我的博客:http://www.cnblogs.com/chruny/p/4791078.html 题目思路: 我上一篇博客解决这个问题的时间复杂度是最坏情况是(O(n^ ...
- 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra
题目描述 FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个 ...
- [LeetCode]题解(python):062 Unique path
题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
随机推荐
- C#操作.csv文件Demo
1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...
- Spring与Struts2整合VS Spring与Spring MVC整合
Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...
- Spring IoC — 基于XML的配置
1.属性注入 注意点: 1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常. 2)JavaBean关于属性命名的特殊规范.Spring只会检查B ...
- 学习webView控件使用
WebView 对象用于网页显示使用,简单的学习并使用了一下. 1.首先在 layout 中摆一个全屏的 webview 控件 (main.xml ) <?xml version="1 ...
- Android开发之EditText属性详解
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- Ios tab Bar 使用方法
http://blog.sina.com.cn/s/blog_63578f140100w56m.html UITabBar* tabBar = [[UITabBar alloc] initWithFr ...
- 传感器(3)传感器的X,Y,Z轴
设备正面水平向上. X轴 : 左右方向,向右是正值. Y轴 : 远近方向,远离你是负. Z轴 : 上下方向,向上是正值.
- BZOJ 2154 Crash的数字表格
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){ ...
- SGU 441 Set Division(矩阵快速幂)
题目链接:http://acm.sgu.ru/status.php 题意:将n个有区别的球放到m个无区别的盒子里,盒子不能为空.不同的方案数. 思路:设f[i][j]表示将前i个球放到j个盒子里,那么 ...
- 转 命令行下玩VC
说明:(1)转载请注明出处:http://www.cnblogs.com/opangle/p/4298155.html (2)以下以VS2013为例,并假设VC安装路径为%VC_INSTALL_PAT ...