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-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-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-24
Algorithem_PermutationInString
Algorithem_PermutationInStringGiven two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1’s permutations is the substring of s2. Example 1: 1234Input: s1 = "ab", s2 = "eidbaooo"Output: trueExplanation: s2 contains one permutation of s1 ("ba"). Example 2: 12Input: s1 = "ab", s2 = "eidboaoo"Output: false 解法首先理解 Permutation的意思,题目需要的是 s2包含 s1中字符串的任意一种排列,就返回 true,否则返...
2022-04-22
Algorithem_MergeTwoSortedLists
Algorithem_MergeTwoSortedListsYou are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: 12Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4] Example 2: 12Input: list1 = [], list2 = []Output: [] Example 3: 12Input: list1 = [], list2 = [0]Output: [0] 解法需求是合并两个有序的LinkList,所以解法是 判断 list1.val 和 list...
2022-04-12
Algorithem_BinarySearch
Algorithem_BinarySearchBinarySearchGiven an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Constraints:All the integers in nums are unique.nums is sorted in ascending order. 解法一开始我的想法是,从每次数组的中间 middleIndex 开始查找,比较数组中间的数字和 target 的大小,target 大则取 从middleIndex到数组结尾截取数组生成新的数组;target 小则取从0到 middle...
2022-04-19
Algorithem_Depth-First Search
Algorithem_Depth-First Search3. Longest Substring Without Repeating CharactersGiven a string s, find the length of the longest substring without repeating characters. Example 1: 123Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: s = "pwwkew"Output: 3Explanation: The answer is "wke&q...
公告
This is my Blog