Help! Runtime Error??


  • 0
    Z

    It was running all right on my PC, but OJ says: Runtime error: Line 24: IndexError: list index out of range. Please help me find out what's wrong? Thanks in advance!!

        def setZeroes(self, matrix):
            if len(matrix) == 0: 
        		return None 
        	if len(matrix[0]) == 0: 
        		return None
        	Rowzerohaszero = False
        	Colzerohaszero = False 
        
        	for j in range(len(matrix[0])): 
        		if matrix[0][j] == 0:
        			 Rowzerohaszero = True
        			 break 
        
        	for i in range (len(matrix)):
        		if matrix[i][0] == 0: 
        			Colzerohaszero = True 
        			break 
        
        	for i in range(1,len(matrix)):
        		for j in range(1,len(matrix[0])): 
        			if matrix[i][j] == 0: 
        				matrix[i][0] = 0
        				matrix[0][j] = 0 
        	
        	for i in range(1,len(matrix)):
        		for j in range(1,len(matrix[0])): 
        			if matrix[i][0] == 0 and matrix[0][j] == 0: 
        				matrix[i][j] = 0 
        				#print i,j
        	
        	if Rowzerohaszero: 
        		for j in range(len(matrix[0])):
        			matrix[0][i] = 0 
        
        	if Colzerohaszero:
        		for i in range (len(matrix)): 
        			matrix[i][0] = 0

  • 0
    R

    where is line 24??


  • 1
    R

    I don't know where is line 24.

    However

       for i in range(1,len(matrix[0])):
            for j in range(1,len(matrix)): 
                if matrix[i][j] == 0: 
                    matrix[i][0] = 0
                    matrix[0][j] = 0 
    

    Your j is the the index for row. Why does it appear at the position of column? It seems to me that you reversed the position of row index and column index in the above loops.

    Copy you code and take a test. It got AC.

    class Solution:
        # @param matrix, a list of lists of integers
        # RETURN NOTHING, MODIFY matrix IN PLACE.
        def setZeroes(self, matrix):
            if len(matrix) == 0: 
                return None 
            if len(matrix[0]) == 0: 
                return None
            Rowzerohaszero = False
            Colzerohaszero = False 
    
            for j in range(len(matrix[0])): 
                if matrix[0][j] == 0:
                     Rowzerohaszero = True
                     break 
    
            for i in range (len(matrix)):
                if matrix[i][0] == 0: 
                    Colzerohaszero = True 
                    break 
    
            for i in range(1,len(matrix)):
                for j in range(1,len(matrix[0])): 
                    if matrix[i][j] == 0: 
                        matrix[i][0] = 0
                        matrix[0][j] = 0 
    
            for i in range(1,len(matrix)):
                for j in range(1,len(matrix[0])): 
                    if matrix[i][0] == 0 or matrix[0][j] == 0: #Should be OR instead of AND
                        matrix[i][j] = 0 
                        #print i,j
    
            if Rowzerohaszero: 
                for j in range(len(matrix[0])):
                    matrix[0][j] = 0 #You had i instead of j
    
            if Colzerohaszero:
                for i in range (len(matrix)): 
                    matrix[i][0] = 0 

  • 0
    Z

    Thanks! I've fixed that, but it still says "list index out of range"...


  • 0
    R

    Fixed it. Take a look.


  • 0
    Z

    Many thanks!! I really appreciate your help and patience. I would spend hours, but cannot find where the bug is. I feel like OJ is like a big family and everyone here is so nice. :)


  • 0
    R

    That's why we are here


  • 0
    Z

    That's sweet! Do you know if there's any offline activities or meetups where people get together, discuss and practice Leetcode problems?


  • 0
    R

    To my knowledge, no offline activities has been held. After all, leetcode only has no more than 4000 users and they are spread globally.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.