Unique Paths 解答

Question

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?

Solution

The key to the solution is to create 2D array to record ways.

 1 public class Solution {
 2     public int uniquePaths(int m, int n) {
 3         int[][] dp = new int[m][n];
 4         dp[0][0] = 0;
 5         for (int i = 0; i < m; i++)
 6             dp[i][0] = 1;
 7         for (int i = 0; i < n; i++)
 8             dp[0][i] = 1;
 9         for (int i = 1; i < m; i++) {
10             for (int j = 1; j < n; j++)
11                 dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
12         }
13         return dp[m - 1][n - 1];
14     }
15 }
时间: 2024-08-11 05:36:30

Unique Paths 解答的相关文章

Unique Paths II 解答

Question 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

LeetCode之“动态规划”:Minimum Path Sum &amp;&amp; Unique Paths &amp;&amp; Unique Paths II

之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or righ

Unique Paths I,II

题目来自于:https://leetcode.com/problems/unique-paths/ :https://leetcode.com/problems/unique-paths-ii/ 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 poi

刷题62. Unique Paths

一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题目,理解后不难.定义一个dp[m][n],初始化最后一列为1,最后一行为1,然后循环计算到dp[0][0]就可以了. 代码如下: class Solution{ public: int uniquePaths(int m,int n){ vector<vector<int>> dp(m

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m 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

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 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 (ma

62.Unique Paths (法1递归-动态规划法2数学公式)

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. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

[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

LintCode : Unique Paths II

Problem Description: 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. Code: public class Solutio