Print the matrix element clockwise (Python implementation)

Source: Internet
Author: User

I think my algorithms are simpler and easier to understand than those on the web. As for the complexity of time, there is no comparison.

Algorithm idea: Traversing the matrix from the outermost to the inner layer

# My algorithm
Import Mathdef Print_matrix (matrix): rec = [] # Store traversal element rows = Len (Matrix) cols = Len (matrix[0]) if rows &L T cols: k = Math.ceil (ROWS/2) else: k = Math.ceil (COLS/2) for N in range (k): # Total number of loops # the top row loop C10/>i = j = N while J < cols: rec.append (Matrix[i][j]) # Print (Matrix[i][j]) J + = 1 # Right-most column follow Ring J-= 1 i + = 1 while i < rows: rec.append (Matrix[i][j]) # Print (matrix[i][j]) i + = 1< c22/># the bottom line loop I-= 1 while j > N: J-= 1 rec.append (Matrix[i][j]) # Print (Matrix[i][j]) c28/># leftmost column loop while i > n+1: i-= 1 rec.append (Matrix[i][j]) # Print (matrix[i][j]) rows- = 1 cols-= 1 return rec

  

Print the matrix element clockwise (Python implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.