LeetCode之旅(31)

来源:互联网 发布:java 可变数组 编辑:程序博客网 时间:2024/06/06 03:42

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?


很经典的题目,不能用递归,会超时


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


0 0
原创粉丝点击