首页 > 代码库 > Unique Paths

Unique Paths

动态规划二位数组

 1 class Solution { 2 public: 3     int uniquePaths(int m, int n) { 4         //c[i][j] = c[i-1][j] + c[i][j-1]; 5         if(m==0 || n==0) 6           return 0; 7         if(m==1 || n==1) 8           return 1; 9         int** c = new int*[m+1];10         for(int k=0;k<m+1;k++)11         {12            c[k] = new int[n+1];13         }14         for(int i=0;i<m+1;i++)15                c[i][0] = 0;16         for(int i=0;i<n+1;i++)17                c[0][i] = 0;18 19 20 21         for(int i=1;i<m+1;i++)22             for(int j=1;j<n+1;j++)23            {24              if(i==1 && j==1)25                  c[i][j] = 1;26              else27                  c[i][j] = c[i-1][j] + c[i][j-1];28            }29         return c[m][n];30     }31 };

 

Unique Paths