[LeetCode]#1030. Matrix Cells in Distance Order

Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]
  1. Calculate distance and related coordinate.
  2. Sort it.
  3. Return it based on distance.
class Solution:
def allCellsDistOrder(self, R, C, r0, c0):
tmp=[]
ans=[]
for i in range(R):
for j in range(C):
tmp.append((abs(r0 - i) + abs(c0 - j), [i, j]))
tmp.sort()
for i in range(len(tmp)):
ans.append(tmp[i][1])
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.