leetcode系列(62)Spiral Matrix, Spiral Matrix II

来源:互联网 发布:贪吃飒淘宝.com 编辑:程序博客网 时间:2024/06/05 09:36

Spiral Matrix

Given amatrix of m x n elements (m rows, n columns),return all elements of the matrix in spiral order.

Forexample,
Given the following matrix:

[

 [ 1, 2, 3 ],

 [ 4, 5, 6 ],

 [ 7, 8, 9 ]

]

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

Spiral Matrix II

Given aninteger n, generate a square matrix filled with elements from 1to n2 in spiral order.

Forexample,
Given n = 3,

Youshould return the following matrix:

[

 [ 1, 2, 3 ],

 [ 8, 9, 4 ],

 [ 7, 6, 5 ]

]

解答:这个题目以前写过一篇博客,用分层转圈的方法,感觉不大通用而且容易出错,对于这一类问题,

m x n或者n x n都可以差不多的模式解决,就是设置row_start,row_end,col_start,col_end,并且在循环(实际也是一层层处理)过程中动态更新。

Spiral Matrix
class Solution {public:    vector<int> spiralOrder(vector<vector<int>>& matrix) {        vector<int> ret;        if (matrix.empty()) {            return ret;        }        int row_start = 0;        int row_end = matrix.size() - 1;        int col_start = 0;        int col_end = matrix[0].size() - 1;                while (true) {            for (int col = col_start; col <= col_end; ++col) {                ret.push_back(matrix[row_start][col]);            }            if (++row_start > row_end) {                break;            }                        for (int row = row_start; row <= row_end; ++row) {                ret.push_back(matrix[row][col_end]);            }            if (--col_end < col_start) {                break;            }                        for (int col = col_end; col >= col_start; --col) {                ret.push_back(matrix[row_end][col]);            }            if (--row_end < row_start) {                break;            }                        for (int row = row_end; row >= row_start; --row) {                ret.push_back(matrix[row][col_start]);            }            if (++col_start > col_end) {                break;            }        }        return ret;    }};
Spiral Matrix II
class Solution {public:    vector<vector<int>> generateMatrix(int n) {        vector<vector<int>> ret(n, vector<int>(n, 0));        if (n <= 0) {            return ret;        }        int row_start = 0;        int row_end = n - 1;        int col_start = 0;        int col_end = n - 1;        int num = 1;                while (true) {            for (int col = col_start; col <= col_end; ++col) {                ret[row_start][col] = num++;            }            if (++row_start > row_end) {                break;            }                        for (int row = row_start; row <= row_end; ++row) {                ret[row][col_end] = num++;            }            if (--col_end < col_start) {                break;            }                        for (int col = col_end; col >= col_start; --col) {                ret[row_end][col] = num++;            }            if (--row_end < row_start) {                break;            }                        for (int row = row_end; row >= row_start; --row) {                ret[row][col_start] = num++;            }            if (++col_start > col_end) {                break;            }        }        return ret;    }};
还有一种针对n x n的解法class Solution {public:    vector<vector<int>> generateMatrix(int n) {        vector<vector<int>> ret(n, vector<int>(n, 0));        if (n <= 0) {            return ret;        }        int start = 0;        int end = n - 1;        int num = 1;        while (start < end) {            for (int col = start; col < end; ++col) {                ret[start][col] = num++;            }            for (int row = start; row < end; ++row) {                ret[row][end] = num++;            }            for (int col = end; col > start; --col) {                ret[end][col] = num++;            }            for (int row = end; row > start; --row) {                ret[row][start] = num++;            }            ++start;            --end;        }        if (start == end) {            ret[start][end] = num;        }        return ret;    }};


0 0
原创粉丝点击