This is the 15th day of my participation in the August Text Challenge.More challenges in August

Spiral matrix II

Given a positive integer n, generate an n x n square matrix matrix containing all elements from 1 to n2n^{2}n2 in a clockwise spiral order.

Examples can be found on the LeetCode website.

Source: LeetCode link: leetcode-cn.com/problems/sp… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

Solution 1: array traversal

First, result is the square matrix to be generated, that is, a two-dimensional array. Accordingly, declare a two-dimensional array of flags of the same size to record whether the corresponding position has been passed, count is the number of recorded elements, I and j record the index position of the current position, I initialized to 0, j initialized to -1, Then start processing two-dimensional arrays in order of right, down, left, and right:

  • To the right: Move j one bit to the right to determine whether it has not crossed the threshold of n and whether the position has not been traversed and count is less than n*n. If so, add count by 1 and fill it to the current(i, j), and marks the position true until it stops moving to the right;
  • Down: Move I down one bit to determine whether the n bound has not been crossed and the position has not been traversed and count is less than n*n. If so, add count by 1 and fill in the current(i, j)Position, and mark the position to true until it is moved down;
  • To the left: Move j one bit to the left to determine whether it is not less than 0 and whether the position has not been traversed and count is less than n*n. If so, add count by 1 and fill it to the current(i, j)And marks the position true until it is no longer moved to the left;
  • Up: Move I up one bit to determine whether it is not less than 0 and whether the position has not been traversed and count is less than n*n. If so, add count by 1 and fill it to the current position(i, j)Position, and marks the position to true until it is no longer moved up.

Repeat the process until count is equal to n*n, that is, all numbers are filled into result, and result is returned.

Description: similar to LeetCode- 054-spiral matrix solution.

public class LeetCode_059 {
    public static int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        boolean[][] flag = new boolean[n][n];
        int i = 0, j = -1, count = 0;
        while (count < n * n) {
            / / to the right
            while (j + 1< n && ! flag[i][j +1] && count < n * n) {
                j = j + 1;
                count++;
                result[i][j] = count;
                flag[i][j] = true;
            }

            / / down
            while (i + 1< n && ! flag[i +1][j] && count < n * n) {
                i = i + 1;
                count++;
                result[i][j] = count;
                flag[i][j] = true;
            }

            / / to the left
            while (j - 1> =0 && !flag[i][j - 1] && count < n * n) {
                j = j - 1;
                count++;
                result[i][j] = count;
                flag[i][j] = true;
            }

            / / up
            while (i - 1> =0 && !flag[i - 1][j] && count < n * n) {
                i = i - 1;
                count++;
                result[i][j] = count;
                flag[i][j] = true; }}return result;
    }

    public static void main(String[] args) {
        for (int[] ints : generateMatrix(4)) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t"); } System.out.println(); }}}Copy the code

Keep your face always with the sunshine for you to let the bad shadows fall behind you.