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) {
//动态规划思想
//选取到本点的最小值(从上方和左方来的最小值)
for(int i = 0; i < grid.length; i++)
for(int j = 0; j < grid[0].length; j++){
if(i > 0 && j > 0)//分三种情况,第二行第二列之后
grid[i][j] += Math.min(grid[i-1][j],grid[i][j-1]);
else if(i == 0 && j > 0)//第一行
grid[i][j] += grid[i][j-1];
else if(i > 0 && j==0)//第一列
grid[i][j] += grid[i-1][j];
}
//返回最后一个值
return grid[grid.length-1][grid[0].length-1];
}
}

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  4. 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 ...

  5. [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 ...

  6. [leetcode] 64. Minimum Path Sum (medium)

    原题 简单动态规划 重点是:grid[i][j] += min(grid[i][j - 1], grid[i - 1][j]); class Solution { public: int minPat ...

  7. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...

  8. 刷题64. Minimum Path Sum

    一.题目说明 题目64. Minimum Path Sum,给一个m*n矩阵,每个元素的值非负,计算从左上角到右下角的最小路径和.难度是Medium! 二.我的解答 乍一看,这个是计算最短路径的,迪杰 ...

  9. 【LeetCode】64. Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

随机推荐

  1. ie6、ie7下JSON.parse JSON未定义的解决方法

    解决方法一: var jsons = req.responseText; var s; if (typeof(JSON) == 'undefined'){ s = eval("(" ...

  2. 微信支付和微信支付通知基于sdk的说明

    前提是,有微信服务号(必须开通了支付功能,也就是说有了商户后台) (注意商户后台 安全目录 的设置,不然即使你写的没错误,也调用不成功) 公众号h5页面写法:  (购物车提交--我们上一步已经生成了订 ...

  3. Atitit.数据库分区的设计 attilax  总结

    Atitit.数据库分区的设计 attilax  总结 1. 分区就是分门别类的文件夹 (what)1 2. 分区的好处(y)1 3. 分区原则(要不要分区,何时分区)how2 4. 主要的分表类型有 ...

  4. 王立平--Eclipse中配置svn

    1.-------------------------------------------------------------------------------------------------- ...

  5. C#动态调用WCF接口(3)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  6. 做自己生活的导演:华为CameraKit为你加持大师光环

    今年最流行的娱乐方式,无疑是短视频,抖音等短视频平台,越来越多的消费者沉浸其中.除了看别人拍的短视频用以丰富生活乐趣之外,也有不少跃跃欲试的消费者加入到短视频拍摄的行列中.随着拍摄者的增加,对拍摄设备 ...

  7. LeetCode543. Diameter of Binary Tree

    Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ...

  8. 什么时候触发MinorGC?什么时候触发FullGC?

    触发MinorGC(Young GC) 虚拟机在进行minorGC之前会判断老年代最大的可用连续空间是否大于新生代的所有对象总空间 1.如果大于的话,直接执行minorGC 2.如果小于,判断是否开启 ...

  9. Java类载入器原理分析

    一:Java虚拟机中能够安装多个类载入器,系统默认是三个基本的类载入器: Bootstrap  ExtClassLoader  AppClassLoader 类载入器也是Java类.由于其它Java类 ...

  10. centos7 笔记本盒盖不睡眠

    cd /etc/systemd vi logind.conf 动作包括:HandlePowerKey:按下电源键后的动作HandleSleepKey:按下挂起键后的动作HandleHibernateK ...