【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 ...
随机推荐
- [转]IA64与X86-64的区别
原文:https://www.cnblogs.com/sunbingqiang/p/7530121.html 说到IA-64与x86-64可能很多人会比较陌生.不知道你在下载系统的时候有没有注意过,有 ...
- c语言单向链表逆转实现方法
自己理解的思路如下所示: 从第二个节点开始,先记录下一个节点,把第二个节点移到头节点之前,头节点变为移动的这个节点之前记录的节点变为接下来要移动的节点用for循环重复最后把原来头节点变成尾节点(*ne ...
- js点击后将文字复制到剪贴板,将图片复制到画图
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD& ...
- 《信息安全技术》实验2——Windows口令破解
实验2 Windows口令破解 在网络界,攻击事件发生的频率越来越高,其中相当多的都是由于网站密码泄露的缘故,或是人为因素导致,或是口令遭到破解,所以从某种角度而言,密码的安全问题不仅仅是技术上的问题 ...
- 实验四android开发基础
实验四android开发基础 提交点一 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd) ...
- 20155308 2016-2017-2 《Java程序设计》第10周学习总结
20155308 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 网络概述 计算机网络:通过一定的物理设备将处于不同位置的计算机连接起来组成的网络,这个网络 ...
- 20155328 《Java程序设计》 实验二(Java面向对象程序设计) 实验报告
20155328 <Java程序设计> 实验二(Java面向对象程序设计) 实验报告 单元测试 一.单元测试和TDD 编程时需理清思路,将编程需求等想好,再开始编.此部分可用伪代码实现. ...
- I/O: std::ios_base::openmode
I/O: std::ios_base::openmode std::ios_base::openmode std::ios_base::in: 打开文件进行读操作,即读取文件中的数据 如果指定路径中 ...
- 【LG3243】[HNOI2015]菜肴制作
题面 洛谷 题解 首先我们有个非常显然的思路, 就是直接拓扑排序,用小根堆代替队列再按顺序输出,但是很显然是错的, 因为这只保证了字典序最小,而无法保证答案最优,\(<2,4>,<3 ...
- 半个小时教你写一个装(bi)逼(she)之地图搜租房
半个小时教你写一个装(bi)逼(she)之地图搜租房 首先需要一个Python3环境,怎么准备我就不多说了,实在不会的出门右转看一下廖雪峰老师的博客. HTML部分 代码来自:高德API+Python ...