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 ...
随机推荐
- 如何在Code First、Database First和Model First之间选择
Code First.Database First和Model First基本图解: 1)Database First: 如果数据库已经存在,可以使用VS自动生成数据模型,已经相关的edmx信息 2) ...
- SQL查询含有%号的字段
select * from EMS_ANNOUNCEMENT where 1=1 and title like '%\%%' escape '\'
- [学习笔记] CDQ分治&整体二分
突然诈尸.png 这两个东西好像都是离线骗分大法... 不过其实这两个东西并不是一样的... 虽然代码长得比较像 CDQ分治 基本思想 其实CDQ分治的基本思想挺简单的... 大概思路就是长这样的: ...
- 寒假关于计算机课程的学习计划(第二次作业<二>)
由于自己刚从紧张的高中学习中解脱出来,进入大学学习意识不够重视,导致学业与别人相差较大,特别是C语言,所以打算寒假主要先补一补C语言.自己在网上查找了很多资料,包括浙江大学翁凯老师在网易云课堂讲的&l ...
- C++课堂作业(2)
github的链接: https://github.com/deepYY/object-oriented/tree/master/PAT.1025 题目 给定一个常数K以及一个单链表L,请编写程序将L ...
- MS10-070 ASP.NET Padding Oracle信息泄露漏洞项目测试
MS10-070 ASP.NET Padding Oracle信息泄露漏洞1 漏洞描述:ASP.NET由于加密填充验证过程中处理错误不当,导致存在一个信息披露漏洞.成功利用此漏洞的攻击 ...
- jq实现 元素显示后 点击页面的任何位置除元素本身外 隐藏元素
$(".share-weixin").on("click",function(e){ var $wx=$(".weixin-share"), ...
- concatenate函数
numpy.concatenate((a1, a2, ...), axis=0) Join a sequence of arrays along an existing axis.(按轴axis连接a ...
- 【zigbee】【蓝牙】射频信号放大器兼容AT2401C
现在科技产品的不断进步,智能家居方面慢慢对信号和距离方面的要求渐渐增加.深圳市动能世纪科技有限公司不断的满足客户需求,推出了一款射频信号放大器AT2401C满足客户距离信号等等的需求.并全方位技术支持 ...
- SDWebImage的一些简单使用
SDWebImage是一个三方类库, 所以要使用它首先要把它引入我们的工程, 其托管在github上: https://github.com/rs/SDWebImage 有几种引入的方法, 一种是直接 ...