Description

A robot is located at the top-left corner of a m x n grid.

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.

How many possible unique paths are there?

m and n will be at most 100.

Example

Given m = 3 and n = 3, return 6.
Given m = 4 and n = 5, return 35.

题目如上,分析题目,从网格的最左上方的一个格子走到指定位置,问有多少种走法?区别于之前做的题目,之前的一道题目问的是最短路径,而这道问的是走法的种类。总的思路是不变的,动态规划嘛。两道题目共同点在于:在走的过程中,只能向右或者向下。有了这个限制条件,题目会变得容易很多。如图所示:
 

网格可以分为两个区域,如图中的红色和绿色,由于不能向左和向上走,所以前往“最左边”和“最右边”的格子(红色格子)只有一种走法。如果想要到达最上面一排的格子,只能向右,同理到达最左面的格子,只能向下。因为是二维的,所以想到可以用一个二维数组来表示到每个点的走法。值得注意的是,传入的参数m,n表示行列数,用数组表示后,右下角的格子为pace[m-1][n-1], 别手抖写成pace[m][n],会越界的 - - (手动滑稽)

代码实现上,也是要分两类处理的,红色的,直接赋值1.  绿色的,要这样考虑,想要走到A号网格,就必须先走到B或者C,那么只要用走到B的方法总数加上走到C的方法总数,就是走到A的方法总数了,具体代码如下。如有错误,欢迎批评指正~

public class Solution {
/**
* @param m: positive integer (1 <= m <= 100)
* @param n: positive integer (1 <= n <= 100)
* @return: An integer
*/
public int uniquePaths(int m, int n) {
// write your code here
int[][]pace=new int[m][n];
for(int i=0;i<m;i++){
pace[i][0]=1;
}
for(int j=0;j<n;j++){
pace[0][j]=1;
}
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
pace[i][j]=pace[i-1][j]+pace[i][j-1];
}
}
return pace[m-1][n-1];
}
}

2018-05-24

114. Unique Paths [by Java]的更多相关文章

  1. Unique Paths leetcode java

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

  2. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. Java for LeetCode 062 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 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). ...

  6. Unique Paths II leetcode java

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  7. LeetCode第[62]题(Java):Unique Paths 及扩展

    题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...

  8. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. 62. Unique Paths (JAVA)

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

随机推荐

  1. haproxy开启日志功能

    haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haproxy使用系统自带的rpm报 ...

  2. .NET正则表达式Regex

    一.IsMatch(Input,patter[,options]) 否则匹配 如果表达式在字符串中匹配,返回布尔值. if (Regex.IsMatch("a.b.c.d", @& ...

  3. 【1】【MOOC】Python游戏开发入门-北京理工大学【第二部分-游戏开发之框架】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  4. URAL-1039 Anniversary Party---树形DP入门题

    题目链接: https://cn.vjudge.net/problem/URAL-1039 题目大意: 开一个party,每个员工都有一个欢乐值,只有是上司和下属不同时存在时才能欢乐,问怎样安排能有最 ...

  5. VMware Harbor 学习

    Harbor简介 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必需的功能特性,例如安全.标识和管理等,扩展了开源Docker Distributio ...

  6. BZOJ1177:[APIO2009]Oil(枚举,前缀和)

    Description 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地为一个矩形区域,被划分为M×N个小块. Siruseri地质 ...

  7. E、CSL 的魔法 【模拟】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)

    题目传送门:https://ac.nowcoder.com/acm/contest/551#question 题目描述 有两个长度为 n 的序列,a0,a1,…,an−1a0,a1,…,an−1和 b ...

  8. [运维笔记] Nginx编译安装

    yum -y install pcre-devel.x86_64 yum -y install openssl openssl-devel.x86_64 useradd www -s /sbin/no ...

  9. C#ref和out的区别-ref是有进有出,out是只出不进

    之前学习C#时候就遇到了这个问题,不过当时没有深究.昨晚想到这个问题时候自己尝试敲了敲代码,结果从运行的结果来看,越看越乱.在查看了一些资料的基础上,自己总结了一下. 可能会有点乱,但是自己总结出来的 ...

  10. Kubernetes(一)--简介

    一.什么是kubernetes(K8s)? Kubernetes作为容器编排生态圈中重要一员,是Google大规模容器管理系统borg的开源版本实现,吸收借鉴了google过去十年间在生产环境上所学到 ...