Algorithem_Matrix
发表于|更新于
|浏览量:
Algorithem_Matrix
题目
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example 1:

1 | Input: mat = [[0,0,0],[0,1,0],[0,0,0]] |
Example 2:

1 | Input: mat = [[0,0,0],[0,1,0],[1,1,1]] |
解法
文章作者: 今是昨非
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 今是昨非的博客!
相关推荐
2022-04-14
Algorithem_MoveZeros
Algorithem_MoveZerosGiven an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: 1234Input: nums = [0,1,0,3,12]Output: [1,3,12,0,0] Example 2: 1234Input: nums = [0]Output: [0] 解法一实现逻辑: 首先把所有非0元素放到前面,并记录长度,最后从非0长度到数组尾部元素置为0 举例如下: 12345678910111213141516nums = [1, 0, 2, 3, 0, 6]j = 0 遍历数组:i = 0,nums[0] = 1, != 0, num[j] = num[i], j + 1, [1, 0, 2, ...
2022-04-20
Algorithem_Depth-First Search
733. Flood FillAn image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also wit...
2022-04-21
Algorithem_Populating Next Right Pointers in Each Node
Algorithem_Populating Next Right Pointers in Each NodeYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition: 123456struct Node { int val; Node *left; Node *right; Node *next;} Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. Example 1: 12Input: root...
2022-04-14
Algorithem_ReverseArray
Algorithem_ReverseArrayGiven an array, rotate the array to the right by k steps, where k is non-negative. Example 1: 12345678Input: nums = [1,2,3,4,5,6,7], k = 3Output: [5,6,7,1,2,3,4]Explanation:rotate 1 steps to the right: [7,1,2,3,4,5,6]rotate 2 steps to the right: [6,7,1,2,3,4,5]rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: 1234567Input: nums = [-1,-100,3,99], k = 2Output: [3,99,-1,-100]Explanation: rotate 1 steps to the right: [99,-1,-100,3]rotate 2 steps to the right: [3,9...
2022-04-15
Algorithem_TwoPointers
Algorithem_TwoPointersTwo Sum II - Input Array Is SortedGiven a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is e...
2022-04-15
Algorithem_ReverseWords
Algorithem_ReverseWordsReverse Words in a String IIIGiven a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: 12Input: s = "Let's take LeetCode contest"Output: "s'teL ekat edoCteeL tsetnoc" Example 2: 12Input: s = "God Ding"Output: "doG gniD" 解法一逻辑:把字符串根据空格切割成数组,然后遍历数组,对数组中字符串调用 reversed 方法,最后在使用空格join为字符串返回 代码如下: 12345678910111213class Solution ...