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. ELBO 与 KL散度

    浅谈KL散度 一.第一种理解 相对熵(relative entropy)又称为KL散度(Kullback–Leibler divergence,简称KLD),信息散度(information dive ...

  2. springboot整合 Thymeleaf模板

    首先引入maven jar依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  3. 【Maven】应用Maven生成jar,包含关联库

    1. java project直接export到处jar包就可以,但在导出的过程中需要指定main class入口. 2. spring boot的项目,应用maven管理库,希望打成jar包,部署到 ...

  4. docker commit

    不能将挂载的外部volume修改的内容一块commit

  5. LDAP注入

    理解LDAP与LDAP注入 0x01 LDAP简介 查阅了一些网上的资料,读了好久还是觉得理解起来很困难,感觉还是不够干,之后看到的一个博客http://www.chinaunix.net/old_j ...

  6. iOS AVPlayer 学习

    1 .使用环境: 在实际开发过程中 有需要展示流媒体的模块 ,需求非常简单 :播放 和 暂停 ,其实这个时候有很多选择 ,可以选择 MPMoviePlayerController(MediaPlaye ...

  7. javascript;select动态添加和删除option

    <select id="sltCity"></select> //添加Option. var optionObj = new Option(text, va ...

  8. grads 读取shp

    自从GrADS2.0.a8版本开始,GrADS引入了对shp图形的支持,关于此格式在这里不多说, 于是今晚就简单测试了一下最简单画图和查询命令(后续还将测试输出shp图形的命令)    测试数据采用的 ...

  9. HTML学习笔记(下)

    表格标签 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...

  10. PCIE phy和控制器

    转:https://wenku.baidu.com/view/a13bc1c20722192e4436f617.html 文章中的第11页开始有划分phy和控制器部分....