leetcode解题方案--062--Unique Paths

来源:互联网 发布:linux运维基础知识 编辑:程序博客网 时间:2024/05/21 20:29

题目

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?

分析

第一反应是动态规划,但是这道题不用也可以,因为动态规划的本质是用空间来记录可能重复计算的值,而这道题并不存在这种情况

另外,学到了一种填充array的方法 arrays.fill

public static int uniquePaths(int m, int n) {        int[][] dp = new int[m][n];        for (int i = 0; i<m;i++) {            for (int j = 0; j<n;j++) {                if (i == 0) {                    dp[i][j] = 1;                } else if (j == 0){                    dp[i][j] = 1;                } else {                    dp[i][j] = dp[i-1][j]+dp[i][j-1];                }            }        }        return dp[m-1][n-1];    }
原创粉丝点击