LeetCode: Unique Paths

一次过

 1 class Solution {

 2 public:

 3     int uniquePaths(int m, int n) {

 4         // Start typing your C/C++ solution below

 5         // DO NOT write int main() function

 6         vector<vector<int>> f(m+1, vector<int>(n+1, 0));

 7         if (m <= 1 || n <= 1) return 1;

 8         for (int i = 1; i <= m; i++) f[i][1] = 1;

 9         for (int i = 1; i <= n; i++) f[1][i] = 1;

10         for (int i = 2; i <= m; i++) {

11             for (int j = 2; j <= n; j++) {

12                 f[i][j] = f[i][j-1] + f[i-1][j];

13             }

14         }

15         return f[m][n];

16     }

17 };

 C#

 1 public class Solution {

 2     public int UniquePaths(int m, int n) {

 3         int[,] f = new int[m+1, n+1];

 4         if (m <= 1 || n <= 1) return 1;

 5         for (int i = 1; i <= m; i++) f[i, 1] = 1;

 6         for (int i = 1; i <= n; i++) f[1, i] = 1;

 7         for (int i = 2; i <= m; i++) {

 8             for (int j = 2; j <= n; j++) {

 9                 f[i, j] = f[i, j-1] + f[i-1, j];

10             }

11         }

12         return f[m, n];

13     }

14 }
View Code

 

你可能感兴趣的:(LeetCode)