The font traversal of a matrix

Source: Internet
Author: User

Title Description: give you a matrix containing M x n elements (m rows, n columns), to find the font traversal of the matrix.

Examples: For the following matrices:

[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]

return [1, 2, 5, 9, 6, 3, 4, 7, 10, 11, 8, 12]


First of all, explain the meaning of the topic first. In fact, the entire matrix is traversed at a 45-degree angle, obliquely upward, alternating diagonally downward. Take the example, as shown in:


Arrows represent the order of traversal.

So, we can design a Boolean up to indicate whether it should be up or down now, but the difficulty with this program is the up conversion.

At first, up = True, go up, and when do you change direction?

To facilitate the explanation, we set the line designator to I, the column label is J:

1. When I < 0 or J >= N, change direction downwards. But the marking also needs to adjust, if J < N, indicates can go right, then J does not change, so i = i-1 (row return), if J >= N (cannot go right), j = j-1 (column homing), I = I-2 (from 8 to 12 in the example)

2. When I >= m or J < 0, change direction to go up, the same way, adjust the label (the difficulty of this problem lies in the adjustment of the label, to analyze carefully)

To figure out the principle, you can give the code:

Class Solution: # @param: a matrix of integers # @return: A list of integers def printzmatrix (self, Matrix):        result, Count = [], 0 if len (matrix) = = 0:return result m, n = Len (Matrix), Len (Matrix[0]) I, j = 0, 0 while count < m * N:up = true when up = = True and I >= 0 and J <            N:result.append (Matrix[i][j]) I-= 1 J + = 1 count + = 1  # can go to the right if J <= n-1: i + = 1 # can't go right, it's just down the road else:i                + = 2 J-= 1 up = False when up = False and I < m and J >= 0:            Result.append (Matrix[i][j]) i + = 1 J-= 1 count + = 1 # can go down                If I <= m-1: J + = 1 # can not go down, you can only go to the right Else:j + 2 I-= 1 returnResult # Write your code here 


The font traversal of a matrix

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.