leetcode 54|59. Spiral Matrix 1|2

来源:互联网 发布:js中的target属性 编辑:程序博客网 时间:2024/06/10 03:11

54. Spiral Matrix

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix 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].

旋转读取二维矩阵,x,y是每一圈的左上角(定点坐标)的横纵坐标,row是每一圈的行数,col是每一圈的列数。

每一圈扫完,定点坐标往右下移动,row-2, col-2

class Solution {public:    void help(vector<vector<int>>& matrix, vector<int> &ret, int x, int y, int row, int col)    {        if (row == 0 || col == 0)                return;        else if (col == 1)        {            for (int i = x; i < x + row; i++)                ret.push_back(matrix[i][y]);            return;        }        else if (row == 1)        {            for (int i = y; i < y + col; i++)                ret.push_back(matrix[x][i]);            return;        }        else        {            for (int i = y; i < y + col; i++)                  ret.push_back(matrix[x][i]);            for (int i = x + 1; i < x + row - 1; i++)                ret.push_back(matrix[i][y + col - 1]);            for (int i = y + col - 1; i >= y; i--)                  ret.push_back(matrix[x + row - 1][i]);            for (int i = x + row - 2; i > x; i--)                ret.push_back(matrix[i][y]);                    help(matrix, ret, x + 1, y + 1, row - 2, col - 2);        }        return;    }        vector<int> spiralOrder(vector<vector<int>>& matrix)     {        vector<int> ret;        if (matrix.size() == 0)  return ret;        int row = matrix.size();        int col = matrix[0].size();        help(matrix, ret, 0, 0, row, col);        return ret;    }};




59. Spiral Matrix II

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 ]
]

way-1:找规律输出就行

class Solution {public:    void Matrix(int n, int pos, int count, vector<vector<int>> &result)    {        //上面一行输入        for (int i = pos; i < n - pos; i++)            result[pos][i] = count ++;                //右边一列        for(int i = pos + 1; i < n - pos - 1; i++)            result[i][n-1-pos] = count ++;                if (count > n * n)  //上面和右边填入之后需要判断,因为可能只有一个坑,而后面可能会重新覆盖            return;                //下面一行        for(int i = n - pos - 1; i >= pos; i--)            result[n-1-pos][i] = count++;                //左边一列        for (int i = n - pos - 2; i > pos; i--)            result[i][pos] = count++;                if (count <= n * n)           Matrix(n, pos+1, count, result);        return;    }        vector<vector<int>> generateMatrix(int n)     {          vector<vector<int>> result;        if (n <= 0)            return result;        else if (n == 1)        {            result.push_back(vector<int> (1,1));            return result;        }        else        {            for(int i=0;i<n;i++)            {                result.push_back(vector<int> (n, 0));            }            Matrix(n, 0, 1, result);        }        return result;            }};




原创粉丝点击