Class Solution
java.lang.Object
g1001_1100.s1030_matrix_cells_in_distance_order.Solution
1030 - Matrix Cells in Distance Order.<p>Easy</p>
<p>You are given four integers <code>row</code>, <code>cols</code>, <code>rCenter</code>, and <code>cCenter</code>. There is a <code>rows x cols</code> matrix and you are on the cell with the coordinates <code>(rCenter, cCenter)</code>.</p>
<p>Return <em>the coordinates of all cells in the matrix, sorted by their <strong>distance</strong> from</em> <code>(rCenter, cCenter)</code> <em>from the smallest distance to the largest distance</em>. You may return the answer in <strong>any order</strong> that satisfies this condition.</p>
<p>The <strong>distance</strong> between two cells <code>(r<sub>1</sub>, c<sub>1</sub>)</code> and <code>(r<sub>2</sub>, c<sub>2</sub>)</code> is <code>|r<sub>1</sub> - r<sub>2</sub>| + |c<sub>1</sub> - c<sub>2</sub>|</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> rows = 1, cols = 2, rCenter = 0, cCenter = 0</p>
<p><strong>Output:</strong> [[0,0],[0,1]]</p>
<p><strong>Explanation:</strong> The distances from (0, 0) to other cells are: [0,1]</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> rows = 2, cols = 2, rCenter = 0, cCenter = 1</p>
<p><strong>Output:</strong> [[0,1],[0,0],[1,1],[1,0]]</p>
<p><strong>Explanation:</strong> The distances from (0, 1) to other cells are: [0,1,1,2]</p>
<p>The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> rows = 2, cols = 3, rCenter = 1, cCenter = 2</p>
<p><strong>Output:</strong> [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]</p>
<p><strong>Explanation:</strong> The distances from (1, 2) to other cells are: [0,1,1,2,2,3]</p>
<p>There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= rows, cols <= 100</code></li>
<li><code>0 <= rCenter < rows</code></li>
<li><code>0 <= cCenter < cols</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[][]
allCellsDistOrder
(int rows, int cols, int rCenter, int cCenter)
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
allCellsDistOrder
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter)
-