@@ -0,0 +1,1463 @@
+# Time:  O(n)%0A# Space: O(n)%0A%0A# Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.%0A# The distance between two adjacent cells is 1.%0A#%0A# Example 1: %0A#%0A# Input:%0A# 0 0 0%0A# 0 1 0%0A# 0 0 0%0A#%0A# Output:%0A# 0 0 0%0A# 0 1 0%0A# 0 0 0%0A#%0A# Example 2: %0A#%0A# Input:%0A# 0 0 0%0A# 0 1 0%0A# 1 1 1%0A#%0A# Output:%0A# 0 0 0%0A# 0 1 0%0A# 1 2 1%0A#%0A# Note:%0A# The number of elements of the given matrix will not exceed 10,000.%0A# There are at least one 0 in the given matrix.%0A# The cells are adjacent in only four directions: up, down, left and right.%0A%0Aclass Solution(object):%0A    def updateMatrix(self, matrix):%0A        %22%22%22%0A        :type matrix: List%5BList%5Bint%5D%5D%0A        :rtype: List%5BList%5Bint%5D%5D%0A        %22%22%22%0A        queue = collections.deque(%5B%5D)%0A        for i in xrange(len(matrix)):%0A            for j in xrange(len(matrix%5B0%5D)):%0A                if matrix%5Bi%5D%5Bj%5D == 0:%0A                    queue.append((i, j))%0A                else:%0A                    matrix%5Bi%5D%5Bj%5D = float(%22inf%22)%0A%0A        dirs = %5B(-1, 0), (1, 0), (0, -1), (0, 1)%5D%0A        while queue:%0A            cell = queue.popleft()%0A            for dir in dirs:%0A                i, j = cell%5B0%5D+dir%5B0%5D, cell%5B1%5D+dir%5B1%5D%0A                if not (0 %3C= i %3C len(matrix)) or not (0 %3C= j %3C len(matrix%5B0%5D)) or %5C%0A                   matrix%5Bi%5D%5Bj%5D %3C= matrix%5Bcell%5B0%5D%5D%5Bcell%5B1%5D%5D+1:%0A                        continue%0A                queue.append((i, j))%0A                matrix%5Bi%5D%5Bj%5D = matrix%5Bcell%5B0%5D%5D%5Bcell%5B1%5D%5D+1%0A        %0A        return matrix%0A

Create 01-matrix.py