zoukankan      html  css  js  c++  java
  • [leedcode 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) {
            //动态规划思想:构造一个dp[][]二维数组,dp[i][j]代表到达i行j列的最少和,
            //当i>0并且j>0时,dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j])+grid[i][j]
            //注意第一行和第一列的处理
            //有一种做法,不需要额外的空间,直接在原有的数组上进行修改数值
            int row=grid.length;
            int col=grid[0].length;
            int dp[][]=new int[row][col];
            for(int i=0;i<row;i++){
                if(i==0) dp[i][0]=grid[i][0];
                else dp[i][0]=grid[i][0]+dp[i-1][0];
                for(int j=1;j<col;j++){
                    if(i==0){
                        dp[i][j]=dp[i][j-1]+grid[i][j];
                    }else{
                        dp[i][j]=Math.min(dp[i][j-1],dp[i-1][j])+grid[i][j];
                    }
                    
                }
            }
            return dp[row-1][col-1];
        }
    }
  • 相关阅读:
    minecraft我的世界汇总网站
    扫雷网页版
    扫雷模型(非完全一样)
    设计模式-策略模式
    hadoop(2)hadoop配置
    hadoop(1)入门
    Openssl
    加密解密
    信息安全通信
    Web
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4643229.html
Copyright © 2011-2022 走看看