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. nodejs开发解决方案

    1.2. 统一环境 开发环境 nvm nrm nodejs 0.10.38 node-inspector 部署环境 nvm nrm iojs 2.x pm2 nginx 异步流程控制:Promise是 ...

  2. CentOS7更改网卡名称为eth0

    #!/bin/bash # Author: fansik # Date: 2017年 09月 19日 星期二 :: CST sed -i 's@rhgb@rhgb net.ifnames=0@g' / ...

  3. Python爬虫:获取新浪网新闻

    代码 #coding:utf-8 import requests from bs4 import BeautifulSoup res = requests.get("http://news. ...

  4. mysql数据库补充知识7 索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  5. java动手动脑解析

    1. 类是java的最小单位,java的程序必须在类中才能运行 2. java函数加不加static有何不同 java中声明为static的方法称为静态方法或类方法.静态方法可以直接调用静态方法,访问 ...

  6. jmeter 测试restful接口

    jmeter 测试restful接口,JSON数据格式 1.添加线程组 2.添加HTTP信息头管理器 请求发送JSON数据格式参数,需要设置Content-Type为application/json ...

  7. 前端基础之css样式(选择器)

    一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...

  8. iOS 什么是函数式编程

    前言:当前只做理解性的常规背书,根据不断深入学习会不断丰富解读内容,欢迎评论提意见 函数式编程:Functional Programming 1 基本解释: 函数式编程 是一种思维模式,一种编程思想, ...

  9. Linux Shell编程 sed命令

    概述 sed 是一种几乎可以应用在所有 UNIX 平台(包括 Linux)上的轻量级流编辑器,体积小.所以,它可以对从如管道这样的标准输入中接收的数据进行编辑. sed 主要是用来将数据进行选取.替换 ...

  10. LVS/DR 配置

    LVS/DR 配置 实验环境 三台主机:Linux Centos 6.4 32位 调度器Director:192.168.1.160(公网IP).192.168.1.100(VIP) HTTP真实服务 ...