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的更多相关文章

  1. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  2. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. 【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 ...

  4. 【leetcode】62. Uniqe Paths

    题目 https://oj.leetcode.com/problems/unique-paths/   A robot is located at the top-left corner of a m ...

  5. 【LeetCode】387. First Unique Character in a String

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/first-unique-cha ...

  6. 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). ...

  7. 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). ...

  8. leetcode@ [62/63] Unique Paths II

    class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...

  9. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

随机推荐

  1. Python学习 :反射 & 单例模式

    反射 什么是反射? - 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省) 面向对象中的反射 - 通过字符串的形式来操作(获取.检查.增加.删除)对象中的成员 - python中的 ...

  2. Python学习 :面向对象(一)

    面向对象 一.定义 面向对象:面向对象为类和对象之间的应用 class + 类名: #在类中的函数称作 “方法“ def + 方法名(self,arg): #方法中第一个参数必须是 self prin ...

  3. NIH周三讲座视频爬虫

    最近网是越来越差了,在线播放基本是没戏了,所以就动了爬虫下载的念头. NIH把视频片段存放,一般都是8秒一段,大概看下视频长度估算一下片段个数就差不多了. 新建一个NIH的文件夹,然后把爬虫下来的.t ...

  4. 【EXCEL】簡単に重複探し

    下記のような表があって.重複があるかどうか探すのが大変と思いますが. 簡単に重複探す方法を紹介します. Step1.重複を探す(例えこちらでは項目)を選択します. Step2.メニューで 条件付き書式 ...

  5. Linux - 时间相关命令 - ntpdate, date, hwclock

    1. 概述 最近也不知道写啥了, 把之前的老文档整理一下, 凑个数什么的 配置时间这种工作, 偶尔还是要用一下 主要描述 3 个命令的简单适用 ntpdate hwlock 2. ntpdate 1. ...

  6. WPF Color、String、Brush转换

    原文:WPF Color.String.Brush转换 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/detai ...

  7. gdb调试带参数程序

    一般来说GDB主要调试的是C/C++的程序.要调试C/C++的程序,首先在编译时,我们必须要 把调试信息加到可执行文件中.使用编译 器(cc/gcc/g++)的 -g 参数可以做到这一点.如: > ...

  8. RHCSA day5

    4.调整逻辑卷容量 请按照以下要求调整本地逻辑卷lvm1的容量: 调整后的逻辑卷及文件系统大小为770MiB 调整后确保文件系统中已存在的内容不能被破坏 调整后的容量可能出现误差,只要在730MiB ...

  9. 【HDU4565】So Easy!

    [HDU4565]So Easy! 题面 要你求 \[ \lceil (a+\sqrt b)^n \rceil \% m \] 其中\(0<a,m<2^{15},(a-1)^2<b& ...

  10. Eclipse--Maven--Dynamic Web Module 3.0 requires Java 1.6 错误

    用Eclipse创建Maven webapp项目时报错Dynamic Web Module 3.0 requires Java 1.6 错误 其实这个问题就是两者不匹配的问题Dynamic Web M ...