114. Unique Paths [by Java]
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]的更多相关文章
- 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). ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- 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). ...
- 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). ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode第[62]题(Java):Unique Paths 及扩展
题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 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 ...
随机推荐
- 沉淀再出发:java中的CAS和ABA问题整理
沉淀再出发:java中的CAS和ABA问题整理 一.前言 在多并发程序设计之中,我们不得不面对并发.互斥.竞争.死锁.资源抢占等等问题,归根到底就是读写的问题,有了读写才有了增删改查,才有了所有的一切 ...
- Centos7中yum安装MySQL
安装mysql [root@localhost ~]# yum update [root@localhost ~]# cat /etc/redhat-release CentOS Linux rele ...
- Java编程规范[转]
目录 1 概述.... 5 1.1 目标:.... 5 1.2 原则:.... 5 2 Java编程命名规范.... 5 2.1 包的命名标准.. ...
- 学习python第一天总纲
1).python基础语法:4周课程(结束阶段考试) 2).前端知识点:html.css.javascript(js).jQuery 3).Linux(系统).数据库(关系型&非关系型) 4) ...
- 【[SCOI2010]序列操作】
好颓啊,我竟然来写了一道恶心的板子 旁边的魏佬嘲讽我,还用欺负我 嘤嘤嘤 那就不膜魏佬了 嘤嘤嘤 这是一道无聊的板子 看到这些操作,我们看到这些操作就知道我们需要维护的东西了 首先那个最长的连续的\( ...
- Django重定向的写法、与直接渲染的区别
Django重定向的写法.与直接渲染的区别 return redirect (“login”) #重定向到login页面,状态码是302页面重定向和直接渲染新的页面的区别.重定向实际是指向了另 ...
- ps基础练习
1. 直接把图片拖进来 2. F 键 视窗全屏快捷键 3. 此时的图片是“背景”层,不能直接编辑,双击该背景层,就变成了“图层”,就可以编辑了 4. 5. 6. 7. 魔棒工具,在需要去掉的点一下, ...
- Spring(六)之自动装配
一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者 ...
- ServletContextListener在Springboot中的使用
ServletContextListener是servlet容器中的一个API接口, 它用来监听ServletContext的生命周期,也就是相当于用来监听Web应用的生命周期.今天我们就来说说如何在 ...
- J2EE从下载到配置成功
1,首先在www.oracle.com网站下载JDK 2 3 4,可以根据自己的需要下载相应的JDK 5,在安装的过程中要将JDK的安装路径和JRE的安装路径安装在不同文件夹中以示区别,例如 6,配置 ...