[Leetcode Notes] spiral matrix II

Source: Internet
Author: User

Given an integerN, Generate a square matrix filled with elements from 1N2 in spiral order.

For example,
GivenN=3,

You shoshould return the following matrix:

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

Problem: the spiral matrix previously worked on is a matrix spiral output, which is a n-spiral matrix. Similar to the first practice, four variables are defined.

Row_left: leftmost boundary of the Child matrix that is not currently filled

Row_right: the rightmost boundary of the Child matrix that is not currently filled

Column_up: top boundary of the Child Matrix

Column_down: bottom boundary of the Child matrix not filled currently

Then, four cycles are carried out each time to create a spiral.

The last element must be processed separately (in fact, only when n is an odd number), because only one element is not filled, and one spiral is not enough to run, if four for loops are used, the elements already placed are modified.

The Code is as follows:

 1 public class Solution { 2     public int[][] generateMatrix(int n) { 3         int matrix[][] = new int[n][n]; 4         int row_up = 0; 5         int row_down = n - 1; 6         int column_left = 0; 7         int column_right = n - 1; 8         int number = 1; 9         10         while(number <= n*n-1){11             for(int i = column_left;i <= column_right;i++){12                 matrix[row_up][i] = number;13                 number++;14             }15             row_up++;16             17             for(int j = row_up;j <= row_down;j++){18                 matrix[j][column_right] = number;19                 number++;20             }21             column_right--;22             23             for(int j = column_right;j >= column_left;j --){24                 matrix[row_down][j] = number;25                 number++;26             }27             row_down--;28             29             for(int i = row_down;i >= row_up;i --){30                 matrix[i][column_left] = number;31                 number++;32             }33             column_left++;34             35         }36         37         if(n > 0)38             matrix[n/2][n%2==0?n/2-1:n/2] = n*n;39         return matrix;40     }41 }

When N is equal to 0, there is no such thing as "the last element is handled independently". In this case, we need to judge the situation. Otherwise, an exception will be thrown.

 

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.