[Leetcode] Unique Paths

来源:互联网 发布:淘宝能部分退款吗 编辑:程序博客网 时间:2024/06/05 17:41

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?

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


0 0
原创粉丝点击