• C#解leetcode 64. 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.

    Note: You can only move either down or right at any point in time.

    利用动态规划的知识求解。从左上开始,遍历到右下。考虑边界情况。

    代码如下:

    public class Solution {
        public int MinPathSum(int[,] grid) {
           
           int row=grid.GetLength(0);
           int col=grid.GetLength(1);
           
           for(int i=0;i<row;i++)
           {
               for(int j=0;j<col;j++)
               {
                   if(i==0&&j!=0)
                   {
                       grid[i,j]=grid[i,j]+grid[i,j-1];
                   }
                   else if(i!=0&&j==0)
                   {
                       grid[i,j]=grid[i,j]+grid[i-1,j];
                   }
                   else if(i==0&&j==0)
                   {
                       grid[i,j]=grid[i,j];
                   }
                   else
                   {
                      grid[i,j]= grid[i,j]+Math.Min(grid[i-1,j],grid[i,j-1]);
                   }
               }
           }
           
           return grid[row-1,col-1];
        }
    }
  • 相关阅读:
    CentOS7.6安装Kubernetes v1.15.1
    数据库三大范式
    linux
    linux
    linux
    linux
    Django contenttypes组件
    Django自带的用户认证
    Django rest framework(7) ---分页
    Django rest framework(6) ---序列化
  • 原文地址:https://www.cnblogs.com/xiaohua92/p/5318456.html
Copyright © 2020-2023  润新知