刷题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,vector<int>(n,0));
            //最后一行初始化为1
            for(int i=0;i<n;i++){
                dp[m-1][i] = 1;
            }

            //最后一列初始化为1
            for(int i=0;i<m;i++){
                dp[i][n-1] = 1;
            }

            for(int t=n-2;t>=0;t--){
                for(int k=m-2;k>=0;k--){
                    dp[k][t] = dp[k+1][t]+dp[k][t+1];
                }
            }

            return dp[0][0];
        }
};

性能如下:

Runtime: 4 ms, faster than 56.78% of C++ online submissions for Unique Paths.
Memory Usage: 8.8 MB, less than 32.81% of C++ online submissions for Unique Paths.

三、优化措施

其他没有看到更好的解答,递归,回溯,都比较复杂。

原文地址:https://www.cnblogs.com/siweihz/p/12248279.html

时间: 2024-11-08 20:52:57

刷题62. Unique Paths的相关文章

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

[Leetcode]@python 62. Unique Paths

题目链接:https://leetcode.com/problems/unique-paths/ 题目大意:给定n.m,在mxn的矩阵中,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走) 解题思路:从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右.也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1) class Solution(object): def uniquePaths(self, m, n): ""&

62. Unique Paths i &amp; ii

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

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 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] 62.Unique Paths I

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

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

62. Unique Paths

题目: A robot is located at the top-left corner of a m x ngrid (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' i

62. Unique Paths (Graph; DP)

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 62. Unique Paths Java

题目: 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'