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

原题链接:https://oj.leetcode.com/problems/unique-paths/

求m*n矩阵的(0,0)到(m,n)有多少种唯一走法,当中每次仅仅能向右或向下走一步。

动态规划状态转移方程:dp[m][n] = dp[m][n-1] + dp[m-1][n],当中dp[m][n]表示到达坐标[m][n]时有多少条唯一的路径

public class UniquePaths {
	public int uniquePaths(int m, int n) {
		if (m == 0 || n == 0)
			return 1;
		int dp[][] = new int[m][n];
		for (int r = 0; r < m; r++)
			dp[r][0] = 1;
		for (int c = 0; c < n; c++)
			dp[0][c] = 1;
		for (int r = 1; r < m; r++) {
			for (int c = 1; c < n; c++)
				dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
		}
		return dp[m - 1][n - 1];
	}
}
时间: 2024-10-25 12:29:00

LeetCode——Unique Paths的相关文章

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution 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 spac

[leetcode]Unique Paths II @ Python

原题地址:https://oj.leetcode.com/problems/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 i

[leetcode]Unique Paths @ Python

原题地址:https://oj.leetcode.com/problems/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 t

[LeetCode] Unique Paths II(DP)

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 middl

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/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 rea

LeetCode——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 middl

[LeetCode]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

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 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 t

leetCode —— Unique Paths II [Dynamic Programming]

唯一路径问题II 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