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网格,求从左上角到右下角的路径条数即可,用dp很容易解决,推到式为res[i][j] = res[i - 1][j] + res[i][j - 1]。意思就是到一个特定点的路径条数是到其左侧或者上侧点的路径条数的总和。

代码如下:

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

还有一种方法是使用dfs来实现,但是数大了就容易超时,这题的本意可能就是dfs,这里把代码放上:

 class Solution {
public:
int uniquePaths(int m, int n) {
times = ;
maxHor = m, maxVer = n;
dfs(,);
return times;
}
void dfs(int hor, int ver){
if((hor == maxHor - ) && (ver = maxVer - ))
times++;
else{
if(hor + < maxHor)  //注意这里不是while
dfs(hor + , ver);
if(ver + < maxVer)
dfs(hor, ver + );
}
}
private:
int times;
int maxHor;
int maxVer;
};

java版本如下所示,dp实现:

 public class Solution {
public int uniquePaths(int m, int n) {
if(m == 0 || n == 0)
return 0;
int [][] grid = new int [m][n];
for(int i = 0; i < m; ++i){
grid[i][0] = 1;
}
for(int i = 0; i < n; ++i){
grid[0][i] = 1;
}
for(int i = 1; i < m; ++i){
for(int j = 1; j < n; ++j){
grid[i][j] = grid[i-1][j] + grid[i][j-1];
}
}
return grid[m-1][n-1];
}
}

LeetCode OJ:Unique Paths(唯一路径)的更多相关文章

  1. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  2. LeetCode 62. Unique Paths不同路径 (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  3. [leetcode]62. Unique Paths 不同路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  4. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  5. 【LeetCode每天一题】Unique Paths(唯一的路径数)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...

  6. LeetCode 62. Unique Paths(所有不同的路径)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  7. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. [LeetCode] 62. Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [Leetcode] unique paths 独特路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  10. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. 001-web基本程序搭建

    一.IDEA创建项目 1.基本项目创建 1.1.基本步骤 1.Create New Project [File→New→Project]→New Project 2.maven→group.artif ...

  2. shuit模块

    shuit模块 #高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中,可以部分内容 def copyf ...

  3. Python之异常处理(Day27)

    一.错误和异常 part1: 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def test: pass #语法错 ...

  4. 统计easyui datagrid某列之和显示在对应列下面

    项目需求要在表格下面加一行统计求和的,结果网上搜寻了一堆,要么说的不详细,高深大牛们的见解:要么实现不了,搜寻老半天修改出一个可以用的,做一下学习记录,新手菜鸟,欢迎指正和新解决方案. 最终效果图: ...

  5. javascript;Jquery;获取JSON对象,无刷新分页,异步加载,异步删除,实例。

    AjaxNewsList: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  6. jQuery UI入门

    jQuery UI是jQuery的一个插件集,为jQuery的核心库添加了新的功能. jQUery UI库可以从http://jquery.com下载. 下载一个ZIP文件jquery-ui-1.9. ...

  7. Linux doxygen的安装与使用

    1.安装doxygen 目前最新版本的的doxygen是doxygen1.8.13,安装包可以在官网上下载,网址是:http://www.stack.nl/~dimitri/doxygen/downl ...

  8. 以太网100Mhz频率为什么可以达到带宽1000Mbps

    转: https://wenku.baidu.com/view/353ea8ecb0717fd5370cdc0b.html

  9. Qt打包过大

    经常看到网上有些论调说 Qt 程序无比庞大,甚至拿 .NET 程序来比,说 Qt 程序打包以后跟 .NET 安装包差不多大.由此影响了很多人对 Qt 的选择.我觉得有必要对此做一些澄清—— 显然这个说 ...

  10. Qt使用QCustomPlot开发

    一.入门 1.下载源文件http://www.qcustomplot.com/: 2.把.cpp和.h放在工程目录下,并将cpp和h加入工程: 3.在.pro中:QT += printsupport: ...