【leetcode】62.63 Unique Paths
62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
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 (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Tips:机器人从左上一直走到右下,(只能走右与下)直到走到FInish的位置。
package medium; import java.util.Arrays; public class L62UniquePaths {
public int uniquePaths(int m, int n) {
int[][] visited = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = -1;
System.out.println(i + "," + j + ">" + visited[i][j]);
}
}
int count = movingCount(m, n, 0, 0, visited);
return count;
}
public int movingCount(int m, int n, int row, int col, int[][] visited) {
int count = 0;
if (row < 0 || col < 0 || row >= m || col >= n)
return 0;
if (row == m - 1 && col == n - 1)
return 1;
if (visited[row][col] != -1)
return visited[row][col];
count = movingCount(m, n, row + 1, col, visited) + movingCount(m, n, row, col + 1, visited);
visited[row][col] = count;
return count;
} //另外一种很快地方法。当前状态依赖于前一种状态
public int Solution2(int m, int n) {
int[] row = new int[n];
Arrays.fill(row,1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
row[j]+=row[j-1];
}
}
return row[n-1];
} public static void main(String[] args) {
L62UniquePaths cc = new L62UniquePaths();
int count = cc.uniquePaths(3, 4);
System.out.println(count);
}
}
63. Unique Paths II
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
Tips:本题目是根据62题,稍作改变得来的,数组中1的位置不能走。
package medium; public class L63UniquePaths2 { public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null)
return 0;
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] visited = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = -1;
System.out.println(i + "," + j + ">" + visited[i][j]);
}
}
int count = movingCount(m, n, 0, 0, visited, obstacleGrid);
return count; } public int movingCount(int m, int n, int row, int col, int[][] visited, int[][] obstacleGrid) {
int count = 0;
if (row < 0 || col < 0 || row >= m || col >= n)
return 0;
if (obstacleGrid[row][col] == 0) {
if (row == m - 1 && col == n - 1)
return 1;
if (visited[row][col] != -1)
return visited[row][col];
count = movingCount(m, n, row + 1, col, visited, obstacleGrid)
+ movingCount(m, n, row, col + 1, visited, obstacleGrid);
visited[row][col] = count;
} return count;
} public static void main(String[] args) {
L63UniquePaths2 l63 = new L63UniquePaths2();
int[][] obstacleGrid = { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } };
int[][] aa = { { 1 } };
int count = l63.uniquePathsWithObstacles(aa);
System.out.println(count); }
}
最新文章
- iOS开发之画图板(贝塞尔曲线)
- PIN码计算锦集
- HTML5 is Canvas
- Caffe学习系列(19): 绘制loss和accuracy曲线
- hdu1358 KMP
- 织梦DedeCMS删除所有栏目或文章后,新建ID不从1开始的解决方法
- TCP连接的状态详解以及故障排查
- 运用.NIT将数据存入数据库、读取数据库(运用封装)陈老师作业
- JavaScript创建命名空间、类及类成员
- Effective c++ 小结
- uva 10900
- 用 EasyBCD 在 Win7/8 中硬盘安装 Ubuntu
- React-Flux 介绍及实例演示
- hdu 1712 ACboy needs your help
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
- PHP之路——MySql基础操作语句
- 随手写的Java向文本文件写字符串的类
- UGUI 帧动画插件
- 听翁恺老师mooc笔记(15)--文件的输入与输出
- python打造一个Mysql数字类型注入脚本(1)
热门文章
- 参考 https://raspberrypi.stackexchange.com/questions/3617/how-to-install-unrar-nonfree >; 1.卸载unrar-free。 $ sudo apt-get remove unrar-free \ 2.通过编辑确保您拥有源存储库/etc/apt/sources.list。 $ cat /etc/apt/sources.
- Centos7最小化安装之工作站设置
- C# 访问修饰符和const、readonly
- 关于第11周课堂mini dc的课堂练习
- c#获取已安装的所有NET版本
- 强化学习读书笔记 - 10 - on-policy控制的近似方法
- .net mvc 使用ueditor的开发(官网没有net版本?)
- Git生成SSH密钥
- SteamVR Unity Plugin - v2.0.1中的InteractionSystem
- 大数据-storm学习资料视频