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 0
Output:
0 0 00 1 00 0 0

Example 2: 
Input:

0 0 00 1 01 1 1
Output:
0 0 00 1 01 2 1

Note:

  1. The number of elements of the given matrix will not exceed 10,000.
  2. There are at least one 0 in the given matrix.
  3. The cells are adjacent in only four directions: up, down, left and right.
这个题目也是室友的启发,可以用上下左右分别扫一次O(4n^2)的时间复杂度即可解决,空间复杂度O(n^2)

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
原创粉丝点击