Unique Paths I

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?

分析:

用A[i][j]表示到达点i,j可能的走法。 对于点A[i][j],它可以从上一个格子下来,也可以从左边那个格子过来。所以A[i][j] = A[i-1][j] + A[i][j-1].

 public class Solution {
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
public int uniquePaths(int m, int n) {
if (n < || m < ) return ;
if (n == || m == ) return ; int[][] paths = new int[m][n]; for (int i = ; i < paths.length; i++) {
for (int j = ; j < paths[].length; j++) {
if (i == || j == ) {
paths[i][j] = ;
} else {
paths[i][j] = paths[i - ][j] + paths[i][j - ];
}
}
}
return paths[m - ][n - ];
}
}

Another solution:

(For clarity, we will solve this part assuming an X+1 by Y+1 grid)

Each path has X+Y steps. Imagine the following paths:

X X Y Y X (we move right on the first 2 steps, then down on the next 2, then right  for the last step)

X Y X Y X (we move right, then down, then right, then down, then right)

Each path can be fully represented by the moves at which we move right. That is, if I were to ask you which path you took, you could simply say “I moved right on step 3 and 4.” Since you must always move right X times, and you have X + Y total steps, you have to pick X times to move right out of X+Y choices. Thus, there are C(X, X+Y) paths (eg, X+Y choose X).

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.

 
Example

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.

分析:

原理同上,没有任何区别。

 public class Solution {
/**
* @param obstacleGrid: A list of lists of integers
* @return: An integer
*/
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null || obstacleGrid.length == || obstacleGrid[].length == ) return ; int m = obstacleGrid.length;
int n = obstacleGrid[].length; int[][] paths = new int[m][n]; for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
if (obstacleGrid[i][j] == ) {
paths[i][j] = ;
} else if (i == && j == ) {
paths[i][j] = ;
} else if (i == ) {
paths[i][j] = paths[i][j - ];
} else if (j == ) {
paths[i][j] = paths[i - ][j];
} else {
paths[i][j] = paths[i - ][j] + paths[i][j - ];
}
}
}
return paths[m - ][n - ];
}
}

Unique Paths | & ||的更多相关文章

  1. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. [LeetCode] Unique Paths 不同的路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. Unique Paths II

    这题在Unique Paths的基础上增加了一些obstacle的位置,应该说增加的难度不大,但是写的时候对细节的要求多了很多,比如,第一列的初始化会受到之前行的第一列的结果的制约.另外对第一行的初始 ...

  5. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  6. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  7. 【leetcode】Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  10. Leetcode Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

随机推荐

  1. ibatis中的$和#的区别

    介绍 在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型匹配,而$不进行数据类型匹配,例如: select * fr ...

  2. POJ 1273 Drainage Ditches -dinic

    dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...

  3. C#获取局域网中的所有正在使用的IP地址

    方法不是很好. using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  4. java连接mysql(三)

    事务的四大特性(ACID) 原子性(Atomicity) 原子性是指事务是一个不可分割的工作单位,事务中的操作要么全部成功,要么全部失败.比如在同一个事务中的SQL语句,要么全部执行成功,要么全部执行 ...

  5. 类,抽象基类,接口类三者间的区别与联系(C++)

    结构上的区别: 普通类:数据+方法+实现 抽象类:数据+方法(一定包含虚方法n>=1)+部分方法的实现 接口类:方法(纯虚方法) http://www.cnblogs.com/Tris-wu/p ...

  6. linux c学习笔记----进程创建(fork,wait,waitpid)

    1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...

  7. mysql 视图(view)

    什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. 数 ...

  8. MySQL的left join中on与where的区别

    关于 “A LEFT JOIN B ON 条件表达式” 的一点提醒 ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行,即使on中包含有A表中的列 ...

  9. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  10. ICMP Internet控制报文协议

    ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制消息.控制消息是指网 ...