Want to pass the matrix to the following code matrix = [[1,1,1],[1,0,1],[1,1,1]]
and print the output (to debug and understand well) just want to know where to pass and print the matrix
class Solution(object):
def setZeroes(self, matrix):
col0 = False
R = len(matrix)
C = len(matrix[0])
for i in range(R):
if matrix[i][0] == 0:
col0 = True
for j in range(1, C):
# If an element is zero, we set the first element of the corresponding row and column to 0
if matrix[i][j] == 0:
matrix[0][j] = 0
matrix[i][0] = 0
# Iterate over the array once again and using the first row and first column, update the elements.
for i in range(1, R):
for j in range(1, C):
if not matrix[i][0] or not matrix[0][j]:
matrix[i][j] = 0
# See if the first row needs to be set to zero as well
if matrix[0][0] == 0:
for j in range(C):
matrix[0][j] = 0
# See if the first column needs to be set to zero as well
if col0:
for i in range(R):
matrix[i][0] = 0
CodePudding user response:
You can call setZeroes
from an instance of Solution
such as
sol = Solution()
sol.setZeroes([[1,1,1],[1,0,1],[1,1,1]])
then you can just print(matrix)
within setZeroes
at any point that you are interested. Or better yet this is a good time to become familiar with an IDE where you can set breakpoints and view the values of local variables.
CodePudding user response:
Since your setZeroes
requires the self
instance and the matrix argument, you can call it by firstly creating an instance of Solution
and then calling its method passing your matrix
:
matrix = [[1,1,1],[1,0,1],[1,1,1]]
Solution().setZeroes(matrix)
print(martix)
Note that setZeroes
modifies the matrix
itself "inplace" (it is a list of lists: it can be done). Therefore, you just need to look at values in matrix
to see the effect.
Another approach could be to copy the matrix
within your setZeroes
method and return
your result.