zoukankan      html  css  js  c++  java
  • 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];
        }
    }
  • 相关阅读:
    python基础 2
    python基础 1
    进程
    进程作业
    上海python14期第二次阶段性考试
    面向对向之元类
    面向对向
    笔试题
    模块(2)
    模块作业
  • 原文地址:https://www.cnblogs.com/xiaohua92/p/5318456.html
Copyright © 2011-2022 走看看