LeetCode: Spiral Matrix

来源:互联网 发布:阅读题软件 编辑:程序博客网 时间:2024/05/01 03:10

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

class Solution {public:    vector<int> spiralOrder(vector<vector<int> > &matrix) {        if(matrix.size() == 0 || matrix[0].size() == 0)            return result;        print(0, matrix.size(), 0, matrix[0].size(), 0, matrix);        return result;    }private:    vector<int> result;    void print(int startX, int endX, int startY, int endY, int turn, vector<vector<int> > &matrix)    {        int x, y;        if(startX >= endX || startY >= endY)            return;        turn = turn % 4;        if(turn == 0)        {            y = startY;            x = startX;            while(y < endY)            {                result.push_back(matrix[x][y]);                y++;            }            print(startX + 1, endX, startY, endY, turn + 1, matrix);        }        else if(turn == 1)        {            y = endY - 1;            x = startX;            while(x < endX)            {                result.push_back(matrix[x][y]);                x++;            }            print(startX, endX, startY, endY - 1, turn + 1, matrix);                    }        else if(turn == 2)        {            y = endY - 1;            x = endX - 1;            while(y >= startY)            {                result.push_back(matrix[x][y]);                y--;            }            print(startX, endX - 1, startY, endY, turn + 1, matrix);                    }        else        {            y = startY;            x = endX - 1;            while(x >= startX)            {                result.push_back(matrix[x][y]);                x--;            }            print(startX, endX, startY + 1, endY, turn + 1, matrix);        }    }};

Round 2:

class Solution {public:    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 x = 0, y = 0, carry = 0;        while(result.size() < m*n)        {            while(x == carry && y < n-carry)            {                result.push_back(matrix[x][y]);                y++;            }            y--;            x++;            if(x >= m-carry)                break;            while(y == n-1-carry && x < m-carry)            {                result.push_back(matrix[x][y]);                x++;            }            x--;            y--;            if(y < carry)                break;            while(x == m-1-carry && y >= carry)            {                result.push_back(matrix[x][y]);                y--;            }            y++;            x--;            if(x <= carry)                break;            while(y == carry && x > carry)            {                result.push_back(matrix[x][y]);                x--;            }            x++;            y++;            carry++;        }        return result;    }};


0 0