Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/d729e146e1268b88f0a9f6dd6094b52f to your computer and use it in GitHub Desktop.
Save jianminchen/d729e146e1268b88f0a9f6dd6094b52f to your computer and use it in GitHub Desktop.
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 0
0 1 0
0 0 0
Output:
0 0 0
0 1 0
0 0 0
Example 2:
Input:
0 0 0
0 1 0
1 1 1
-1 -1 -1
-1 0 -1
-1 -1
Output:
0 0 0
0 1 0
1 2 1
keywords:
matrix, value only 0 and 1,
statement: the distance two adjacent cell is 1
left, right, up or down , at most 4 adajcent cells
ask: find distance of nearest 0 for each cell
if cell value is 0, no change
if cell value is 1,
0 0 0
0 1 0
0 0 0
0 0 0
0 1 0
1 2 1 <--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment