[LeetCode] Unique Paths

来源:互联网 发布:爱心机构手机网站源码 编辑:程序博客网 时间:2024/06/12 21:03
int uniquePaths(int m, int n) {int** path = new int*[m];for (int i = 0; i < m; i++){path[i] = new int[n];}for(int r = 0, c = 0; r < m; r++){path[r][c] = 1;        }for(int r = 0, c = 0; c < n; c++){path[r][c] = 1;        }for(int r = 1; r < m; r++){for(int c = 1; c < n; c++){path[r][c] = path[r-1][c] + path[r][c-1];}}return path[m-1][n-1];}


最初,这么定义数组:int path[m][n]; VS会报这些错:error C2057: 应输入常量表达式,error C2466: 不能分配常量大小为 0 的数组。

因此,改为给数据动态分配内存。

0 0
原创粉丝点击