Leetcode#63 Unique Paths II

原题地址

 

基本动态规划题

 

代码:

 1 int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
 2         if (obstacleGrid.empty() || obstacleGrid[0].empty()) return 0;
 3         
 4         int m = obstacleGrid.size();
 5         int n = obstacleGrid[0].size();
 6         vector<int> sum(n, 0);
 7         
 8         sum[n - 1] = 1;
 9         for (int i = m - 1; i >= 0; i--) {
10             sum[n - 1] = obstacleGrid[i][n - 1] > 0 ? 0 : sum[n - 1];
11             for (int j = n - 2; j >= 0; j--)
12                 sum[j] = obstacleGrid[i][j] > 0 ? 0 : sum[j] + sum[j + 1];
13         }
14                 
15         return sum[0];
16 }

 

posted @ 2015-01-27 18:31  李舜阳  阅读(139)  评论(0编辑  收藏  举报