【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);
}
}
【leetcode】62.63 Unique Paths的更多相关文章
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】62. Unique Paths
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- 【leetcode】62. Uniqe Paths
题目 https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- 【LeetCode】387. First Unique Character in a String
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...
- 62. 63. Unique Paths 64. Minimum Path Sum
1. 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
题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
随机推荐
- Shell学习积累//持续更新
1.until的使用 直到判断条件满足,否则会一直执行,与while使用相反 until [ $command -eq 200 ] do command=`curl -o /dev/null -s - ...
- [WC2010][BZOJ1758]重建计划-[二分+分数规划+点分治]
Description 传送门 Solution 看到那个式子,显然想到分数规划...(不然好难呢) 然后二分答案,则每条边的权值设为g(e)-ans.最后要让路径长度在[L,U]范围内的路径权值&g ...
- 成都Uber优步司机奖励政策(4月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【HEOI2016】排序
题面 题解 这题好神仙啊... 我们二分这个位置上的数, 然后当\(val[i] \geq mid\)的位置设为\(1\),否则为\(0\) 这样一来,这道题就变成了一个\(01\)序列排序,所以就可 ...
- OpenStack入门篇(四)之KVM虚拟机介绍和管理
1.查看虚拟机,启动虚拟机 [root@linux-node1 ~]# virsh list --all Id Name State --------------------------------- ...
- 洛谷P2831 愤怒的小鸟
洛谷P2831 愤怒的小鸟 原题链接 题解 首先简单数学公式送上. \(ax_1^2+bx_1=y_1\) \(ax_2^2+bx_2=y_2\) \(ax_1^2x_2+bx_1x_2=y_1x_2 ...
- 欢迎使用 Flask¶
欢迎使用 Flask¶ 欢迎阅读 Flask 文档. 本文档分为几个部分.我推荐您先从 安装 开始,之后再浏览 快速入门 章节. 教程 比快速入门更详细地介绍了如何用 Flask 创建一个完整的 应用 ...
- 【Unity3d】WWW类发起web连接
初学unity3d,解决一个游戏与web服务器连接问题. 看了项目中原始代码,发现每次之前的程序员每次调用WWW类都需要写一遍StartCoroutine,然后各种重复代码. 于是写了一个简单的封装类 ...
- 移动端车牌识别/车牌OCR识别
周末,小编约了朋友商场shopping. 开车进地下车库时,“滴”的一声,完成车牌录入:开车离开时,扫描二维码,输入车牌,完成停车收费.小编不禁感叹科技改变生活,人工智能给生活带来的便利. 车牌自动识 ...
- Java 验证码识别库 Tess4j 学习
Java 验证码识别库 Tess4j 学习 [在用java的Jsoup做爬虫爬取数据时遇到了验证码识别的问题(基于maven),找了网上挺多的资料,发现Tess4j可以自动识别验证码,在这里简单记录下 ...