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 ...
随机推荐
- codeforces 388D Fox and Perfect Sets(线性基+数位dp)
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp mak ...
- Vim 编辑器及其基本操作
实验楼某些课程有用 Vim 编辑器来写代码,因此有了这篇博客,据说是上古神器,当然主要目的是基本操作. Vim 编辑器 Vim(Vi IMprove) 是 Linux 系统上的最著名的文本/代码编辑器 ...
- 动画的分类:属性(几何)动画、内容(视频)动画:gpu vs cpu
属性动画通过gpu根据属性来呈现: 内容动画通过cpu解码内容按照时间呈现给gpu: (或者gpu直接解码现实?)
- C++作用域 (二)
http://www.cnblogs.com/wolf-lifeng/p/3156936.html 2.3全局作用域 2.3.1概述 全局作用域是最大的名字空间作用域,不同于用户自定义的名字空间作用域 ...
- 【[HEOI2016/TJOI2016]字符串】
码农题啊 上来先无脑一个\(SA\)的板子,求出\(SA\)和\(het\)数组 我们只需要从\(sa[i]\in[a,b]\)的所有\(i\)中找到一个\(i\)使得\(sa[i]\)和\(rk[c ...
- Python常用库之三:Matplotlib
导入模块 import matplotlib.pyplot as plt import seaborn as sb 绘制条形图 countplot(data:数据集, x:x坐标轴, color:条形 ...
- Bootstrap--模仿官网写一个页面
本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...
- PAT——1021. 个位数统计
给定一个k位整数N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),请编写程序统计每种不同的个位数字 ...
- Go语言之旅:基本类型
原文地址:https://learn-linux.readthedocs.io 欢迎关注我们的公众号:小菜学编程 (coding-fan) Go 内置了以下基本类型: 布尔 bool 字符串 stri ...
- .net core 实践笔记(二)--EF连接Azure Sql
** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/9902098.html 笔者使用了常见的三层架构,Api展示层注入了Swa ...