国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

LeetCode 2711. Difference of Number of Distinct Values on Diagon

2023-05-29 08:53 作者:您是打尖兒還是住店呢  | 我要投稿

Given a?0-indexed?2D?grid?of size?m x n, you should find the matrix?answer?of size?m x n.

The value of each cell?(r, c)?of the matrix?answer?is calculated in the following way:

  • Let?topLeft[r][c]?be the number of?distinct?values in the top-left diagonal of the cell?(r, c)?in the matrix?grid.

  • Let?bottomRight[r][c]?be the number of?distinct?values in the bottom-right diagonal of the cell?(r, c)?in the matrix?grid.

Then?answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|.

Return?the matrix?answer.

A?matrix diagonal?is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end.

A cell?(r1, c1)?belongs to the top-left diagonal of the cell?(r, c), if both belong to the same diagonal and?r1?< r. Similarly is defined bottom-right diagonal.

?

Example 1:

Input: grid = [[1,2,3],[3,1,5],[3,2,1]]

Output: [[1,1,0],[1,0,1],[0,1,1]]

Explanation:?

The 1st diagram denotes the initial grid.?

The 2nd diagram denotes a grid for cell (0,0), where blue-colored cells are cells on its bottom-right diagonal.?

The 3rd diagram denotes a grid for cell (1,2), where red-colored cells are cells on its top-left diagonal.

The 4th diagram denotes a grid for cell (1,1), where blue-colored cells are cells on its bottom-right diagonal and red-colored cells are cells on its top-left diagonal.?

- The cell (0,0) contains [1,1] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |1 - 0| = 1.

- The cell (1,2) contains [] on its bottom-right diagonal and [2] on its top-left diagonal. The answer is |0 - 1| = 1.?

- The cell (1,1) contains [1] on its bottom-right diagonal and [1] on its top-left diagonal. The answer is |1 - 1| = 0. The answers of other cells are similarly calculated.

Example 2:

Input: grid = [[1]]

Output: [[0]]

Explanation:?

- The cell (0,0) contains [] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |0 - 0| = 0.

?

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 1 <= m, n, grid[i][j] <= 50

這里面是不包括當(dāng)前單元格的,所以要i-1 j-1或者i+1 j+1,

依次遍歷即可,寫了2個(gè)函數(shù),一個(gè)判斷左上,一個(gè)判斷右下;

下面是代碼:

Runtime:?27 ms, faster than?40.00%?of?Java?online submissions for?Difference of Number of Distinct Values on Diagonals.

Memory Usage:?44.9 MB, less than?20.00%?of?Java?online submissions for?Difference of Number of Distinct Values on Diagonals.


LeetCode 2711. Difference of Number of Distinct Values on Diagon的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
武清区| 班玛县| 城口县| 望都县| 吴桥县| 海晏县| 甘洛县| 武城县| 启东市| 财经| 个旧市| 宁乡县| 东辽县| 乌什县| 兴安县| 靖江市| 静宁县| 弋阳县| 元氏县| 芦山县| 鄂伦春自治旗| 宁陵县| 新疆| 井研县| 肥东县| 绿春县| 拜泉县| 封开县| 平定县| 永川市| 临清市| 沅江市| 甘肃省| 定日县| 北票市| 汶上县| 绵阳市| 墨脱县| 阿坝| 辽阳县| 遂川县|