LEETCODE54/59 Spiral Matrix I/ii

Source: Internet
Author: User

One: Spiral Matrix I

Topic:

Given a matrix of m x n elements (m rows, n columns), return all elements of the Matri X in Spiral Order.

For example,
Given the following matrix:

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

You should return [1,2,3,6,9,8,7,4,5] .

Links: https://leetcode.com/problems/spiral-matrix/

Analysis: Look at the code, four while loops form a large circle

Class Solution {public:void DFS (int xi, int yi, const int &m, const int &n, vector<vector<int> > &        Amp;matrix, vector<int> &result, int **visited) {Visited[xi][yi] = 1;        Result.push_back (Matrix[xi][yi]);            while (Yi+1 < n &&!visited[xi][yi+1]) {//Four while loop just ran the Big Circle Result.push_back (Matrix[xi][yi+1]);            VISITED[XI][YI+1] = 1;        Yi = yi+1;            } while (Xi+1 < M &&!visited[xi+1][yi]) {result.push_back (Matrix[xi+1][yi]);            Visited[xi+1][yi] = 1;        Xi = XI +1;            } while (Yi-1 >=0 &&!visited[xi][yi-1]) {result.push_back (matrix[xi][yi-1]);            VISITED[XI][YI-1] = 1;        Yi = yi-1;            } while (Xi-1 >=0 &&!visited[xi-1][yi]) {result.push_back (Matrix[xi-1][yi]);            Visited[xi-1][yi] = 1;        Xi = xi-1; } if (Yi+1 < n &&!visited[xi][yi+1]) Dfs (xi, yi+1, M, N, Matrix, result, visited);        } vector<int> Spiralorder (vector<vector<int> > &matrix) {vector<int> result;        int m = Matrix.size ();                if (M = = 0) return result;        int n = matrix[0].size ();        int **visited= new INT*[M];            for (int i = 0; i < m; i++) {Visited[i] = new Int[n];        memset (Visited[i], 0, sizeof (int) *n);                } dfs (0, 0, m, N, Matrix, result, visited);        for (int i = 0; i < m; i++) Delete [] visited[i];        delete []visited;                    return result; }};

II: Spiral Matrix II

Topic:

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]]
Links: https://leetcode.com/problems/spiral-matrix-ii/

Class Solution {public:void DFS (int xi, int yi, const int &n, vector<vector<int> > &result, int &A         Mp;value) {Result[xi][yi] = value++;            while (Yi+1 < n &&!result[xi][yi+1]) {//Four while loop just ran the big circle result[xi][yi+1] = value++;        Yi = yi+1;            } while (Xi+1 < n &&!result[xi+1][yi]) {Result[xi+1][yi] = value++;        Xi = xi+1;            } while (yi-1 >= 0 &&!result[xi][yi-1]) {result[xi][yi-1] = value++;        Yi = yi-1;            } while (xi-1 >= 0 &&!result[xi-1][yi]) {Result[xi-1][yi] = value++;        Xi = xi-1;    } if (Yi+1 < n &&!result[xi][yi+1]) Dfs (xi, yi+1, n, result, value);        } vector<vector<int> > Generatematrix (int n) {vector<vector<int> >result;        if (n = = 0) return result; for (int i = 0; i < n; i++) {//Initialize result VECTor<int> temp (n, 0);        Result.push_back (temp);        } int value = 1;        DFS (0,0, n, result, value);            return result; }};


LEETCODE54/59 Spiral Matrix I/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.