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]。意思就是到一个特定点的路径条数是到其左侧或者上侧点的路径条数的总和。

代码如下:

  1. class Solution {
  2. public:
  3. int uniquePaths(int m, int n) {
  4. vector<vector<int> > ans(m, vector<int>(n, ));
  5. for(int i = ; i < m; ++i)
  6. ans[i][] = ;
  7. for(int j = ; j < n; ++j)
  8. ans[][j] = ;
  9.  
  10. for(int i = ; i < m; ++i){
  11. for(int j = ; j < n; ++j){
  12. ans[i][j] = ans[i - ][j] + ans[i][j - ];
  13. }
  14. }
  15. return ans[m - ][n - ];
  16. }
  17. };

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

  1. class Solution {
  2. public:
  3. int uniquePaths(int m, int n) {
  4. times = ;
  5. maxHor = m, maxVer = n;
  6. dfs(,);
  7. return times;
  8. }
  9. void dfs(int hor, int ver){
  10. if((hor == maxHor - ) && (ver = maxVer - ))
  11. times++;
  12. else{
  13. if(hor + < maxHor)  //注意这里不是while
  14. dfs(hor + , ver);
  15. if(ver + < maxVer)
  16. dfs(hor, ver + );
  17. }
  18. }
  19. private:
  20. int times;
  21. int maxHor;
  22. int maxVer;
  23. };

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

  1. public class Solution {
  2. public int uniquePaths(int m, int n) {
  3. if(m == 0 || n == 0)
  4. return 0;
  5. int [][] grid = new int [m][n];
  6. for(int i = 0; i < m; ++i){
  7. grid[i][0] = 1;
  8. }
  9. for(int i = 0; i < n; ++i){
  10. grid[0][i] = 1;
  11. }
  12. for(int i = 1; i < m; ++i){
  13. for(int j = 1; j < n; ++j){
  14. grid[i][j] = grid[i-1][j] + grid[i][j-1];
  15. }
  16. }
  17. return grid[m-1][n-1];
  18. }
  19. }

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. mysql学习笔记—常用sql函数

    SQL 拥有很多可用于计数和计算的内建函数. SQL Aggregate 函数 SQL Aggregate 函数计算从列中取得的值,返回一个单一的值. 有用的 Aggregate 函数: AVG() ...

  2. FSR薄膜压力传感器使用教程

    FSR薄膜压力传感器教程 本店常用的外形有2种: 圆形: 长条形: 如果用单片机控制建议买带转换的,可以直接接单片机AD口或者数字IO去读取数值: 电压输出的AO接口是模拟量输出,可以接单片机的模拟口 ...

  3. 【读书笔记】Java核心技术-基础知识-反射

    在网页中运行Java程序称为applet. 反射 这项功能被大量地应用于JavaBeans中,它是Java组件的体系结构. 能够分析类能力的程序称为反射(reflective).反射机制的功能及其强大 ...

  4. Android开发问题:ActivityNotFoundException: Unable to find explicit activity class

    http://blog.csdn.net/debuglog/article/details/7236013 原因:AndroidManifest.xml未添加对应Activity配置. 解决办法:在A ...

  5. 前端 初级篇(HTML)

    HTML 概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就 ...

  6. PAT 天梯赛 L1-034. 点赞 【MAP】

    题目链接 https://www.patest.cn/contests/gplt/L1-034 AC代码 #include <cstdio> #include <cstring> ...

  7. PAT 天梯赛 L1-038. 新世界 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-038 AC代码 #include <iostream> #include <cstdio&g ...

  8. keepalived nginx 主备配置

    keepalived  nginx 主备配置(多主多备同理) 1.Nginx服务安装 nginx 不区分主备,在两台服务上安装两个即可. 安装参考:https://www.cnblogs.com/zw ...

  9. 20145210姚思羽《网络对抗》Web基础

    20145210姚思羽<网络对抗>Web基础 实验后回答问题 (1)什么是表单 表但是与用户交互的窗口,负责采集网页中的数据,允许用户在表单中输入信息. (2)浏览器可以解析运行什么语言. ...

  10. iOS_SDWebImage框架分析

    SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的 category,方便使用.使用SDWebImage首先了解它加载图片的流程. 入口 setIma ...