leetcode
来源:互联网 发布:时间分配软件 编辑:程序博客网 时间:2024/06/05 17:00
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.
Solution1:
better
public int minPathSum(int[][] grid) { int width = grid[0].length; int height = grid.length; for (int i = 1; i < width; i++) { grid[0][i] += grid[0][i - 1]; } for (int i = 1; i < height; i++) { grid[i][0] += grid[i - 1][0]; for (int j = 1; j < width; j++) { grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); } } return grid[height - 1][width - 1]; }
Solution2:
public int minPathSum(int[][] grid) { int width = grid[0].length; int height = grid.length; for (int i = 0; i < height; i++) { for (int j = 0; j < width; 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] = Math.min(grid[i][j - 1], grid[i - 1][j]) + grid[i][j]; } } } return grid[height - 1][width - 1]; }
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- nrm -- NPM registry 管理工具
- Unity WorldToScreenPoint坐标变换
- 使用Java语言开发微信公众平台(三)——被关注回复与关键词回复
- 【图像处理】GeoTiff Tags
- hibernate配置文件hibernate.cfg.xml和.hbm.xml的详细解释
- leetcode
- POJ1860Currency Exchange
- java判断对象是否是数组
- 分布式事务之说说TCC事务
- 正则表达式语法
- leetcode
- ]String... 与 String[]区别
- 【图像处理】PLUS XMP Tags
- 寒冰王座 HDU