LEEDCODE 54螺旋矩阵

LEEDCODE 54螺旋矩阵_第1张图片

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int up = 0;
        int m = matrix.size();
        int n = matrix[0].size();
        int down = m - 1;
        int left = 0;
        int right = n - 1;
        std::vector<int> a;
        while(true)
        {
            // shang
            for(int i = left; i <= right; i++)
                a.push_back(matrix[up][i]);
            up += 1;
            if(up > down)
                break;
            
            for(int i = up; i <= down; i++)
                a.push_back(matrix[i][right]);
            right -= 1;
            if(right < left)
                break;

            for(int i = right; i >= left; i--)
                a.push_back(matrix[down][i]);      
            down -= 1;
            if(down < up)
                break;

            for(int i = down; i >= up ; i--)
                a.push_back(matrix[i][left]);
            left += 1;
            if(right < left)
                break;            
        }
        return a;
    }
};

LEEDCODE 54螺旋矩阵_第2张图片
LEEDCODE 54螺旋矩阵_第3张图片

你可能感兴趣的:(矩阵,线性代数)