剣指offer python版29.時計回りのマトリックス

20110 ワード

'''
      ,                       ,
  ,        :
[[ 1,  2,  3,  4],
 [ 5,  6,  7,  8],
 [ 9, 10, 11, 12],
 [13, 14, 15, 16]]
         1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
'''
class Solution:
    # matrix       ,      
    def printMatrix(self, matrix):
        if matrix == None:
            return
        rows = len(matrix)
        columns = len(matrix[0])
        start = 0
        while rows > start * 2 and columns > start *2:
            self.PrintMatrixInCircle(matrix, columns, rows, start)
            start += 1
        print('')

    def PrintMatrixInCircle(self, matrix, columns, rows, start):
        endX = columns - 1 - start
        endY = rows - 1 - start

        #         
        for i in range(start, endX +1):
            number = matrix[start][i]
            print(number, ' ', end='')

        #         
        if start < endY:
            for i in range(start + 1, endY + 1):
                number = matrix[i][endX]
                print(number, ' ', end='')

        #         
        if start < endX and start < endY:
            for i in range(endX-1, start-1, -1): #      -1       start-1
                number = matrix[endY][i]
                print(number, ' ', end='')

        #         
        if start < endX and start < endY - 1:
            for i in range(endY-1, start, -1):
                number = matrix[i][start]
                print(number, ' ', end='')

    #                
    def PrintMatrix1(self, matrix):
        printArr = []
        if matrix == None:
            return
        if matrix == []:
            return []
        start = 0
        rows = len(matrix)
        columns = len(matrix[0])

        while columns > 2 * start and rows > 2 * start:
            endX = columns - 1 - start
            endY = columns - 1 - start

            #          printArr
            for i in range(start, endX + 1):
                number = matrix[start][i]
                printArr.append(number)

            #          printArr
            if start < endY:
                for i in range(start + 1, endY + 1):
                    number = matrix[i][endX]
                    printArr.append(number)

            #          printArr
            if start < endX and start < endY:
                for i in range(endX - 1, start - 1, -1):
                    number = matrix[endY][i]
                    printArr.append(number)

            #          printArr
            if start < endX and start < endY - 1:
                for i in range(endY-1, start, -1):
                    number = matrix[i][start]
                    printArr.append(number)
            start += 1
        return printArr

if __name__ == '__main__':
    matrix = [[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16]]
    matrix2 = [[1], [2], [3], [4], [5]]
    matrix3 = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
    S = Solution()
    S.printMatrix(matrix)
    S.printMatrix(matrix2)
    S.printMatrix(matrix3)

    print(S.PrintMatrix1(matrix))