LeetCode 题解(6):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].

题解:

不一定是最优解。

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int> > &matrix) {


        vector<int> spiralMatrix;
        if( matrix.empty() )
            return spiralMatrix;
            
        int lastColumn = matrix[0].size()-1;
        int lastRow = matrix.size()-1;    
        
        int firstColumn = 0, firstRow = 0;
        int flag = 0;
        int direction;
        
        if(lastColumn == 0 && lastRow == 0)
        {
            spiralMatrix.push_back(matrix[0][0]);
            return spiralMatrix;
        }
        
        while(!(lastRow < firstRow || lastColumn < firstColumn))
        {
            direction = flag % 4;
            switch(direction)
            {
                case 0:
                {
                    for( int i = firstColumn; i <= lastColumn; i++ )
                    {
                        spiralMatrix.push_back(matrix[firstRow][i]);
                    }
                    firstRow++;
                    break;
                }
                case 1:
                {
                    for( int i = firstRow; i <= lastRow; i++ )
                    {
                        spiralMatrix.push_back(matrix[i][lastColumn]);
                    }
                    lastColumn--;
                    break;
                }
                case 2:
                {
                    for( int i = lastColumn; i >= firstColumn; i-- )
                    {
                        spiralMatrix.push_back(matrix[lastRow][i]);
                    }
                    lastRow--;
                    break;
                }
                case 3:
                {
                    for( int i = lastRow; i >= firstRow; i-- )
                    {
                        spiralMatrix.push_back(matrix[i][firstColumn]);
                    }
                    firstColumn++;
                    break;
                }
                default:
                {
                    break;
                }
                    
            }
            flag++;
        }
        return spiralMatrix;
        
        
    }
};

测试用例:

#include <vector>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
	vector<vector<int>> a;
	int row1[] = {1,2,3}, row2[] = {4,5,6}, row3[] = {7,8,9};
	vector<int> r1(row1, row1+sizeof(row1)/sizeof(int));
	vector<int> r2(row2, row2+sizeof(row2)/sizeof(int));
	vector<int> r3(row3, row3+sizeof(row3)/sizeof(int));
	Solution s;
	a.push_back(r1);
	a.push_back(r2);
	a.push_back(r3);

	vector<int> c = s.spiralOrder(a);

	cout << "[";
	for(vector<int>::iterator i = c.begin(); i < c.end(); i++)
	{
		cout << *i;
		if(i != c.end()-1)
			cout << ",";
	}
	cout << "]" << endl;

	return 0;
}


你可能感兴趣的:(LeetCode,Algorithm,SpiralMatrix)