542. 01 Matrix
https://leetcode.com/problems/01-matrix/
BFS问题,第一感觉是使用队列来做。先对原数组进行处理,将 0 的下标入队列,非零下标记为最大Integer值。之后由每个0的四周逐渐散开,覆盖掉其四周的 Integer.MAX_VALUE。这样对于处理过的坐标,后续其他 0 散开达到时,其距离一定比当前值大,也就不用处理了。
1 | public int[][] updateMatrix(int[][] matrix) { |
https://leetcode.com/problems/01-matrix/
BFS问题,第一感觉是使用队列来做。先对原数组进行处理,将 0 的下标入队列,非零下标记为最大Integer值。之后由每个0的四周逐渐散开,覆盖掉其四周的 Integer.MAX_VALUE。这样对于处理过的坐标,后续其他 0 散开达到时,其距离一定比当前值大,也就不用处理了。
1 | public int[][] updateMatrix(int[][] matrix) { |
jsonContent:
meta: false
pages: false
posts:
title: true
date: true
path: true
text: false
raw: false
content: false
slug: false
updated: false
comments: false
link: false
permalink: false
excerpt: false
categories: true
tags: true