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 ...
随机推荐
- Linux watch命令详解
watch可以帮你监测一个命令的运行结果,来监测你想要的一切命令的结果变化 常见命令参数 Usage: watch [-dhntv] [--differences[=cumulative]] [--h ...
- 查看oracle数据库最近执行了哪些sql语句
SELECT b.sql_text, --content of SQL a.machine, --which machine run this code a.username, a.module, - ...
- [DP]洛谷P1115最大子段和
题目来源 https://www.luogu.org/problemnew/show/P1115 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一行是一 ...
- Salesforce平台支持多租户Multi tenant的核心设计思路
Multitenancy is the fundamental technology that clouds use to share IT resources cost-efficiently an ...
- 利用Fiddler2和Proxifier分析你用的中国菜刀是否带有后门
为了避免自己辛辛苦苦拿下的站点被一些拿来主义者不费吹灰之力就据为己有,下面来教大家如何检测菜刀有没有留后门. 对于有没有后门这个问题,大牛们会说抓包看一下就行了,那如何抓包呢?有很多软件可以,这里使用 ...
- BZOJ 1202 狡猾的商人 差分约束or带权并查集
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1202 题目大意: 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的 ...
- Mac版Mysql Workbench不展示Schema
Mac版的Mysql Workbench会不展示Schema,如下图 操作如下 cd ~/Library/Application\ Support/MySQL/Workbench/ rm wb_sta ...
- 【js】五子棋-UI学习
http://www.imooc.com/learn/639 棋盘用canvas实现
- 4、Android-数据存储方案(文件存储/SharedPreferences)
简介: 任何一个应用程序都需要和数据进行交互 对于软件来说微博.QQ等都需要关系的是数据 没有数据的应用程序是一个没有灵魂的软件 而且还没有实际的用途 可以认为是单机 4.1.持久化技术的简介 数据持 ...
- html5物理定位误差大 解决办法
学生党在做比赛作品,项目中需求要用到定位功能并以地图形式展现.所以思路就是用h5的geolocation 获取经纬度,通过百度地图api将经纬度转换成详细的地址以及地图.在笔记本电脑做测试,定位总有超 ...