Leetcode-spiral matrix II Spiral Matrix 2 python Dafa good, four lines will be done, you believe?

Source: Internet
Author: User

Spiral Matrix II

Spiral Matrix

Given an integer n, generate a square matrix filled with elements from 1 to n2 in Spiral order.

For example,
Given N = 3 ,

You should return the following matrix:

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

 

It's not easy. Look at the blog Park someone interviewed encountered this problem, long-winded to see me dizzy. Run to the Leetcode discussion area, it seems to see or I Python Dafa, four lines will be done you believe.

| 1 | 2 | 3 |

| 8 | 9 | 4 |

| 7 6 | 5 |

If you don't see it, the general process is like this, and the same color means it's executed in the same step.

>>> def generatematrix (n): ...     A, low = [], n*n+1     ... While low > 1: ...         Low, High = Low-len (A), low ...         A = [List (range low, high)] + list (Zip (*a[::-1))  # python3.3 syntax differs from 2.7         ... Print (A)     ... Return a...>>> Generatematrix (1) [[]][[1]][[1]]>>> Generatematrix (2) [[]][[4]][[3],  (4,)][[1, 2],  (4, 3)][[1, 2], (4, 3)]>>> Generatematrix (3) [[]][[9]][[8],  (9,)][[6, 7],  (9, 8)][[4, 5],  (9, 6),  (8, 7)] [[1, 2, 3],  (8, 9, 4),  (7, 6, 5)][[1, 2, 3], (8, 9, 4), (7, 6, 5)]

Leetcode-spiral matrix II Spiral Matrix 2 python Dafa good, four lines will be done, you believe?

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.