码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode的medium题集合(C++实现)十一

时间:2015-05-23 11:32:57      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   动态规划   

1 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?
因为只能向右和向下移动,所以在(i,j)位置的上一个位置只能是(i?1,j)(i,j?1),用path[i][j]表示从开始位置到当前位置的路径和,则有path[i][j]=path[i?1][j]+path[i][j?1]

int uniquePaths(int m, int n) {
        vector<vector<int> > path(m,vector<int>(n,0));
        //初始化顶部和左边的格子
        for(int i=0;i<m;i++) path[i][0]=1;
        for(int j=0;j<n;j++) path[0][j]=1;
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                path[i][j]=path[i-1][j]+path[i][j-1];
            }
        }
        return path[m-1][n-1];
    }

利用上一轮的计算结果,可以将二维数组降为一维

int uniquePaths(int m, int n) {
        vector<int> path(n,0);
        for(int j=0;j<n;j++) path[j]=1;
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                path[j]=path[j]+path[j-1];
            }
        }
        return path[n-1];
    }

2 Unique Paths II
Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.
与上一题一样,用path[i][j]表示从开始位置到当前位置的路径和。区别是要判断是否有障碍,如果有这将path[i][j]置0,没障碍时则有path[i][j]=path[i?1][j]+path[i][j?1]

int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m=obstacleGrid.size();
        int n=obstacleGrid[0].size();
        vector<vector<int> > path(m,vector<int>(n,0));
        path[0][0]=obstacleGrid[0][0]==1? 0:1;
        for(int i=1;i<m;i++) path[i][0]=obstacleGrid[i][0]==1? 0:path[i-1][0];
        for(int j=1;j<n;j++) path[0][j]=obstacleGrid[0][j]==1? 0:path[0][j-1];
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                path[i][j]=obstacleGrid[i][j]==1? 0:(path[i-1][j]+path[i][j-1]);
            }
        }
        return path[m-1][n-1];
    }

3 Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
与第一题方法相似

int minPathSum(vector<vector<int>>& grid) {
        int m=grid.size();
        int n=grid[0].size();
        vector<vector<int> > path(m,vector<int>(n,0));
        path[0][0]=grid[0][0];
        for(int i=1;i<m;i++) path[i][0]=path[i-1][0]+grid[i][0];
        for(int j=1;j<n;j++) path[0][j]=path[0][j-1]+grid[0][j];
        for(int i=1;i<m;i++)
        {
            for(int j=1;j<n;j++)
            {
                path[i][j]=grid[i][j]+min(path[i-1][j],path[i][j-1]);
            }
        }
        return path[m-1][n-1];
    }

LeetCode的medium题集合(C++实现)十一

标签:c++   leetcode   动态规划   

原文地址:http://blog.csdn.net/zhulong890816/article/details/45932615

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!