Leetcode-542. 01 Matrix
来源:互联网 发布:网络捕鱼游戏犯法吗 编辑:程序博客网 时间:2024/06/06 04:14
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。这次比赛略无语,没想到前3题都可以用暴力解。
博客链接:mcf171的博客
——————————————————————————————
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.Example 1:
Input:
0 0 00 1 00 0 0Output:
0 0 00 1 00 0 0
Example 2:
Input:
0 0 00 1 01 1 1Output:
0 0 00 1 01 2 1
Note:
- The number of elements of the given matrix will not exceed 10,000.
- There are at least one 0 in the given matrix.
- The cells are adjacent in only four directions: up, down, left and right.
public class Solution { public List<List<Integer>> updateMatrix(List<List<Integer>> matrix) { List<List<Integer>> rows = new ArrayList<List<Integer>>(); for(int i = 0; i < matrix.size(); i ++){ List<Integer> temp = new ArrayList<Integer>(); for(int j = 0; j < matrix.get(i).size(); j ++) temp.add(Integer.MAX_VALUE); rows.add(temp); } if(matrix.size() == 0) return rows; int[] rowDistance = new int[matrix.size()]; int[] columnDistance = new int[matrix.get(0).size()]; int[] dx = new int[]{0,matrix.size() - 1}; int[] dy = new int[]{0,matrix.get(0).size() - 1}; for(int i = 0; i < 2; i++){ for(int j = 0; j < 2; j++){ init(rowDistance, columnDistance); for(int row = dx[i]; row >= 0 && row < matrix.size(); row = row + 1 + i * -2){ int[] minDistance = new int[]{ 0, Integer.MAX_VALUE}; for(int column = dy[j]; column >=0 && column < matrix.get(0).size(); column = column + 1 + j * -2) help(matrix,row,column,minDistance,rowDistance,columnDistance, rows); } } } return rows; } public void init(int[] rowDistance, int[] columnDistance){ for(int i = 0; i < rowDistance.length; i ++) rowDistance[i] = Integer.MAX_VALUE; for(int i = 0; i < columnDistance.length; i ++) columnDistance[i] = Integer.MAX_VALUE; } public void help(List<List<Integer>> matrix, int i, int j, int[] minDistance, int[] rowDistance, int[] columnDistance, List<List<Integer>> rows){ int element = matrix.get(i).get(j); if(element == 0){ rows.get(i).set(j,0); rowDistance[i] = j; columnDistance[j] = i; minDistance[0] = j; minDistance[1] = 0; } else{ int rowMin = Math.min(rows.get(i).get(j), Math.abs(rowDistance[i] - j)); int columnMin = Math.min(rows.get(i).get(j), Math.abs(columnDistance[j] - i)); int miner = Math.min(rowMin,columnMin); int currentDistance = minDistance[1] == Integer.MAX_VALUE ? Integer.MAX_VALUE : Math.abs(minDistance[0] - j) + minDistance[1]; if(currentDistance < miner){ rows.get(i).set(j,currentDistance); } else{ minDistance[0] = j; minDistance[1] = miner; rows.get(i).set(j,miner); } } }}
0 0
- Leetcode-542. 01 Matrix
- [LeetCode]542. 01 Matrix
- leetcode 542. 01 Matrix
- [leetcode]542. 01 Matrix
- 【LeetCode】542. 01 Matrix
- 542. 01 Matrix [LeetCode]
- [LeetCode] 542. 01 Matrix
- leetcode 542. 01 Matrix
- LeetCode 542. 01 Matrix
- LeetCode 542. 01 Matrix
- 【leetcode】542. 01 Matrix的解法总结
- [Leetcode] 542. 01 Matrix 解题报告
- leetcode-542-01 Matrix
- [LeetCode.542]01 Matrix
- LeetCode之01 Matrix
- LeetCode--01 Matrix
- 【LeetCode】01 Matrix 解题报告
- [Leetcode] #542 01 Matrix (BFS)
- Android Studio 多渠道打包改名(超简单版)
- 逃逸闭包(@escaping)和非逃逸闭包
- Java后台接口开发初步实战
- javascript encodeURI和encodeURIComponent的比较
- CSS 浮动(float)注意点
- Leetcode-542. 01 Matrix
- 【Java】JDBC基础及应用
- 学习Linux系统的方法经验
- struts2(复合)集合数据校验
- Microsoft Speech_Note 语音识别模块使用
- linux环境下文件权限解析
- context.obtainStyledAttributes 研究
- PHP代码注释规范
- GAT保险模块的修改