Spiral Matrix

来源:互联网 发布:淘宝第三方平台有哪些 编辑:程序博客网 时间:2024/06/06 16:18

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

Hide Tags
 Array




 

solution

同Spiral Matrix II

class Solution {public:    vector<int> spiralOrder(vector<vector<int>>& matrix) {                vector<int> ret;        if(matrix.empty() || matrix[0].empty())            return ret;                    int m = matrix.size();        int n = matrix[0].size();        if(m == 1 && n == 1)        {            ret.push_back(matrix[0][0]);            return ret;        }                int rowBegin = 0;        int rowEnd = m - 1;        int colBegin = 0;        int colEnd = n - 1;                        while (rowBegin <= rowEnd && colBegin <= colEnd)        {            //左-->右            for(int i = colBegin;i<= colEnd;i++)            {                ret.push_back(matrix[rowBegin][i]);            }            rowBegin++;                        //上-->下            for(int i = rowBegin;i<= rowEnd;i++)            {                ret.push_back(matrix[i][colEnd]);            }            colEnd--;                        //右-->左,加if判断,避免重复            if(rowBegin <= rowEnd)            {                for(int i = colEnd; i>= colBegin;i--)                {                    ret.push_back(matrix[rowEnd][i]);                }                rowEnd--;            }                        //下-->上,加if判断,避免重复            if(colBegin <= colEnd)            {                                for(int i = rowEnd;i>=rowBegin;i--)                {                    ret.push_back(matrix[i][colBegin]);                }                colBegin++;                    }                    }                return ret;            }};



对于每一层(记为层i),以(i,i)位置出发,向右到达(i,n-1-i),向下到达(m-1-i,n-1-i),向左到达(m-1-i,i),再向上回到起点。

所有层次遍历完成后,即得到所求数组

注意:i==m-1-i 或者 i==n-1-i时,不要来回重复遍历。



//http://www.cnblogs.com/ganganloveu/p/4157376.html//剑指offer还是哪里有这个题,解法似乎没这么复杂class Solution {public:    vector<int> spiralOrder(vector<vector<int> > &matrix) {        vector<int> ret;        if(matrix.empty() || matrix[0].empty())            return ret;                int m = matrix.size();        int n = matrix[0].size();        int layer = (min(m,n)+1) / 2;        for(int i = 0; i < layer; i ++)        {            //row i: top-left --> top-right            for(int j = i; j < n-i; j ++)                ret.push_back(matrix[i][j]);                            //col n-1-i: top-right --> bottom-right            for(int j = i+1; j < m-i; j ++)                ret.push_back(matrix[j][n-1-i]);                            //row m-1-i: bottom-right --> bottom-left            if(m-1-i > i)            {                for(int j = n-1-i-1; j >= i; j --)                    ret.push_back(matrix[m-1-i][j]);            }                            //col i: bottom-left --> top-left            if(n-1-i > i)            {                for(int j = m-1-i-1; j > i; j --)                    ret.push_back(matrix[j][i]);            }        }        return ret;    }};


0 0
原创粉丝点击