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 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

解题思路:

Climbing Stairs二维版。计算解个数的题多半是用DP。而这两题状态也非常显然,dp[i][j]表示从起点到位置(i, j)的路径总数。DP题目定义好状态后,接下去有两个任务:找通项公式,以及确定计算的方向。
1. 由于只能向右和左走,所以对于(i, j)来说,只能从左边或上边的格子走下来:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
2. 对于网格最上边和最左边,则只能从起点出发直线走到,dp[0][j] = dp[i][0] = 1
3. 计算方向从上到下,从左到右即可。可以用滚动数组实现。
 
Java Solution 1:
class Solution {
public int uniquePaths(int m, int n) {
if (m == 0 || n == 0) {
return 1;
} int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
} for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}

Java Solution 2:

class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
int i, j;
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++ j) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
}
else {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}
}

CPP:

class Solution {
public:
/**
* @param n, m: positive integer (1 <= n ,m <= 100)
* @return an integer
*/
int uniquePaths(int m, int n) {
// wirte your code here
vector<vector<int> > f(m, vector<int>(n)); for(int i = 0; i < n; i++)
f[0][i] = 1; for(int i = 0; i < m; i++)
f[i][0] = 1; for(int i = 1; i < m; i++)
for(int j = 1; j < n; j++)
f[i][j] = f[i-1][j] + f[i][j-1]; return f[m-1][n-1];
}
};

Python:

class Solution(object):
def uniquePaths(self, m, n):
dp = [[0] * n for i in xrange(m)]
for i in xrange(m):
for j in xrange(n):
if i == 0 or j == 0:
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] return dp[m -1][n - 1]  

Python: Time: O(m * n) Space: O(m + n)

class Solution:
# @return an integer
def uniquePaths(self, m, n):
if m < n:
return self.uniquePaths(n, m)
ways = [1] * n for i in xrange(1, m):
for j in xrange(1, n):
ways[j] += ways[j - 1] return ways[n - 1] 

Python:

class Solution:
# @return an integer
def c(self, m, n):
mp = {}
for i in range(m):
for j in range(n):
if(i == 0 or j == 0):
mp[(i, j)] = 1
else:
mp[(i, j)] = mp[(i - 1, j)] + mp[(i, j - 1)]
return mp[(m - 1, n - 1)] def uniquePaths(self, m, n):
return self.c(m, n)

Python: wo

class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
dp = [[0] * n for i in xrange(m)] #  m, n不能反了
for i in xrange(m):
for j in xrange(n):
if i == 0 and j == 0:
dp[i][j] = 1
elif i == 0:
dp[i][j] = dp[i][j-1]
elif j == 0:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[-1][-1]  

JavaScript:

/**
* @param m: positive integer (1 <= m <= 100)
* @param n: positive integer (1 <= n <= 100)
* @return: An integer
*/
const uniquePaths = function (m, n) {
var f, i, j;
f = new Array(m);
for (i = 0; i < m; i++) f[i] = new Array(n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (i === 0 || j === 0) {
f[i][j] = 1;
} else {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
}
return f[m - 1][n - 1];
}

   

 

  

  

[LeetCode] 62. Unique Paths 唯一路径的更多相关文章

  1. LeetCode 62. Unique Paths不同路径 (C++/Java)

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

  2. [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). The ...

  3. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  4. [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). The ...

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

  6. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

  7. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  8. 62. Unique Paths不同路径

    网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...

  9. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

随机推荐

  1. 摘:JAVA JXL API的详细使用

    转自:http://www.cr173.com/html/10377_1.html 1 开发调研1.1 需求描述MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为 ...

  2. 微信小程序~事件绑定和冒泡

    [1]事件绑定和冒泡 事件绑定的写法同组件的属性,以 key.value 的形式. key 以bind或catch开头,然后跟上事件的类型,如bindtap.catchtouchstart.自基础库版 ...

  3. .NET 使用 VLC 播放视频

    使用 VLC 播放监控有几个月了,现在是多个项目中都有用到.在使用的过程中也有一些细节供大家参考. 一.对 VLC 的了解 VLC 是一个开源的跨平台多媒体播放器及框架. VLC 官方出的有播放器.编 ...

  4. MP4文件批量转码成MP3

    需求背景:最近为了学python爬虫,在论坛里找了不少视频教程,非常棒.但有时看视频不方便,就想着能否把视频批量转码成音频,这样在乘坐地铁公交的时候也能学习了. 解决路径:有了需求,我首先在论坛里搜了 ...

  5. CodeForces - 76F:Tourist (旋转坐标系,LIS)

    pro:有一个驴友,以及给定N个表演地点xi和时间ti,驴友的速度不能超过V. 问他在起点为原点和不设置起点的情况下分别最多参观多少个表演. sol:BZOJ接飞饼见过:clari也在camp的DP专 ...

  6. 题解 LA2889

    题目大意 多组数据,每组数据给出一个正整数 \(n\),输出第 \(n\) 大的回文数(即 \(1,2,3,\cdots\)). 分析 不难发现,\(n\) 位的回文数有 \(9*10^{\lfloo ...

  7. 解决:一个项目中写多个包含main函数的源文件并分别调试运行

    自己在学c++的时候,一个项目中的多个cpp文件默认不允许多个main函数的出现,但是通过选项操作能够指定单个cpp文件进行运行,如下: 1.此时我就想运行第二个cpp文件,我们只需要把其他的两个右键 ...

  8. AD域与信任关系

    域与信任关系:信任关系分为两种,一种是林中信任关系,另一种是林之间的信任关系. 林中信任关系的特点: 注意:林中信任关系还可以分为两种:一种是父子信任,还有一种是树根信任. 父子信任:在同一个树域之中 ...

  9. LOJ P10130 点的距离 题解

    这道题相当于倍增求LCA的板子,我们只要构建一棵树,然后距离就是x的深度+y的深度 - LCA(x,y)的深度: #include<iostream> #include<cstdio ...

  10. 不用VS调试.Net

    将来,任何开发人员都将需要调试应用程序,并且将无法访问Visual Studio,在某些情况下甚至无法访问源代码.例如,在生产web或应用服务器上调试问题时,我真的不想安装Visual Studio并 ...