"Leetcode-Interview algorithm classic-java implementation" "059-spiral Matrix II (Spiral Matrix II)"

Source: Internet
Author: User

"059-spiral Matrix II (Spiral Matrix II)" "leetcode-Interview algorithm classic-java Implementation" "All Topics folder Index" Original Question

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:

123894765 ]]

Main Topic

Given an integer n. Generates a n*n matrix, which is filled with 1-n^2 numbers.


Thinking of solving problems

The calculation method is used to calculate the corresponding number for each position.


Code Implementation

Algorithm implementation class

 Public  class solution {     Public int[][]Generatematrix(intN) {int[[] result =New intN [n];intLayerintK for(inti =0; I < n; i++) { for(intj =0; J < N; J + +) {layer = Layer (I, J, N);///current coordinates are several levels away                //n * n-layer * The last digit (also the largest) used by the outer layer of the layer                the first number used by the current layer where the coordinates are locatedk = n * N-(N-2* Layer) * (N-2* Layer) +1; RESULT[I][J] = k;//(N-2 * layer-1): Four (N-2 * layer-1) is the total number of elements (x, y) in the same layer                if(i = = layer) {//Case One, the coordinates are near the upper boundaryRESULT[I][J] = k + J-layer; }Else if(j = = N-layer-1) {//Situation two, coordinates off the right edge of the recentRESULT[I][J] = k + (N-2* Layer-1) + I-layer; }Else if(i = = N-layer-1) {//Case three, the coordinates are near the lower boundaryRESULT[I][J] = k +3* (N-2* Layer-1)-(J-layer); }Else{//Case three, the coordinates are near the left edgeRESULT[I][J] = k +4* (N-2* Layer-1)-(I-layer); }            }        }returnResult }/** * In a n*n matrix, calculate the number of layers outside the (x, y) coordinates, the coordinates start at 0 * * @param x Horizontal axis * @param y ordinate * @ param n Matrix size * Number of layers outside @return coordinates */     Public int Layer(intXintYintN) {x = x < n-1-X?

x : n - 1 - x; // 计算横坐标离上下边界的近期距离 y = y < n - 1 - y ? y : n - 1 - y; // 计算纵坐标离左右边界的近期距离 return x < y ? x : y; // 较小的值为坐标的外围层数 }}

Assessment Results

  Click on the picture, the mouse does not release, drag a position, release after the new form to view the full picture.

Special Instructions Welcome reprint. Reprint Please specify the source "http://blog.csdn.net/derrantcm/article/details/47164439"

"Leetcode-Interview algorithm classic-java implementation" "059-spiral Matrix II (Spiral Matrix II)"

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.