Ask for critiques on my codes


  • 0
    B

    I think it can be simplified more. Thank you. The running time should be O(row+column).

    Here are the codes:

    class Solution:
    # @param matrix, a list of lists of integers
    # @param target, an integer
    # @return a boolean
    def searchMatrix(self, matrix, target):
    if len(matrix)==1:
    single_line=matrix[0]
    for k in range(len(single_line)):
    if target==single_line[k]:
    return True
    break
    else:
    continue
    return False
    else:
    first_row=matrix[0]
    last_row=matrix[-1]
    if len(first_row)==0:
    return False
    elif target<first_row[0] or target>last_row[-1]:
    return False
    elif target>=first_row[0] and target<=matrix[-2][-1]:
    for j in range(len(matrix)-1):
    if target>=matrix[j][0] and target<matrix[j+1][0]:
    for i in range(len(matrix[j])):
    if target<>matrix[j][i]:
    continue
    else:
    return True
    break
    return False
    else:
    continue
    else:
    for j in range(len(last_row)):
    if target==last_row[j]:
    return True
    break
    else:
    continue
    return False


Log in to reply
 

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