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. iOS机型适配

           机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的 ...

  2. Codeforces Round #240 (Div. 1)B---Mashmokh and ACM(水dp)

    Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university ...

  3. .align

    .align的作用是针对指令或数据的存放地址对齐.但不同的CPU架构,指令和数据的存储方式不同,也就导致对齐的计量单位不一样. i386:n对齐 ARM:2^n 对齐,ARM架构下,指令都是占32位, ...

  4. docker的使用01

    使用Dockerfile构建镜像 vim dockerfile01 #注释信息 FROM ubuntu:latest //导入镜像 MAINTAINER leo "leo@leo.com&q ...

  5. Tomcat闲聊第二话

    windows下安装Tomcat可以直接下载Installer,需要注意的是,先确保安装了Java虚拟机. 注:默认安装路径为 C:\Program Files\Apache Software Fou ...

  6. layui表格渲染中模板的使用举例

    实例一: { field: 'status', align: 'center', title: '活动状态', templet: function (d) { if (d.status == &quo ...

  7. UI-10-plist文件及UITableView的高级应用①

    课程要点: plist文件的新建与读取 给UITableView设置变化的值 单元格的删除.插入及刷新 plist文件的新建与读取 新建plist Commadn+N,iOS->Resouce- ...

  8. VM603:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1

    再用JQuery解析json的时候出现了这样一个问题 VM603: Uncaught SyntaxError: Unexpected token o 通过查阅资料发现,是由于解析json文件的时候解析 ...

  9. VLC WebPlugin中文

    Documentation:WebPlugin 这篇文档讲述的是 VLC media player Web plugins 和怎样在网页使用它 Contents 1 介绍: 构建包含video的Web ...

  10. ThinkPHP 处理商品添加的时候操作多张表 用事务解决。

    #重新父类的add方法 public function add(){ #同时操作多装表,可以考虑用事务来做,要同时插入数据成功要么都不插输入数据. #开启事务的前提是表的引擎必须是InnoDB #开启 ...