Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to reverse the output Matrix of JZ19

Shulou Source: shulou.com Published: 2022-06-01 02:30:26 10月05日 Update

This article mainly shows you "JZ19 how to reverse the output matrix", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to reverse the output matrix of JZ19" this article.

The problem of JZ19 inverse output Matrix

Enter a matrix and print each number in clockwise order from outside to in. for example, if you enter the following 4 X 4 matrix: 12 3 4 5 6 6 7 8 9 10 11 12 13 14 15 16, then print out the numbers 1, 2, 4, 4, 4, 8, 4, 12, 16, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 9, 5, 6, 7, 11, 11, 10.

Train of thought

Locate the upper left and lower right coordinates of the current circle matrix

Code #-*-coding:utf-8-*-class Solution: # matrix type is a 2D list You need to return a list of def printMatrix (self, matrix): # Positioning left, upper and lower right point coordinates x1, y1, x2, y2 = 0,0, len (matrix [0]), len (matrix) res = [] while x2 > x1 and y2 > y1: for i in range (y1) X2): res.append (Matrix [x1] [I]) if y2-y1==1: break for i in range (x1x1, y2-1): res.append (Matrix [I] [x2-1]) for i in range (x2-1, y1-1) -1): res.append (Matrix [y2-1] [I]) if x2-x1==1: break for i in range (y2-2, x1,-1): res.append (Matrix [I] [y1]) x1, y1, x2, y2 = x1, y1, x2-1 Y2-1 return res if _ _ name__ = ='_ _ main__': matrix = [[1Power2 _ name__ = ='_ 4], [5 ~ 6 ~ 6 ~ 7 ~ 8], [13 ~ 14 ~ 14 ~ 15 ~ 16],] matrix2 = [1 ~ 2 ~ 2 ~ 3 ~ 4 ~ 4], [6 ~ 7 ~ 8 ~ 9 ~ 10], [11 ~ 12 ~ 13 ~ 14 ~ 15],] matrix3 = [1 ~ 2 ~ 3], [1 ~ 2] Matrix4 = [1], [2], [3], [4], [5] sl = Solution () print (sl.printMatrix2 (matrix4)), these are all the contents of the article "how to reverse JZ19 output Matrix" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Matrix reverse order output content article coordinates numbers learning positioning help input extroversion code ideas hour hand easy to understand more organization knowledge type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS vpn NVidia Microsoft