顺时针打印数组

/*
    20 :> 顺时针打印数组
*/
vector<int> printMatrix(vector<vector<int> > matrix) {
    vector<int>res;
    res.clear();
    int row = matrix.size();//行数
    int col = matrix[0].size();//列数

    int circle = ((row < col ? row : col) - 1) / 2 + 1;//圈数

    for (int i = 0; i < circle; i++)
    {
        //zuo->you
        for (int j = i; j < col - i; ++j)
            res.push_back(matrix[i][j]);
        //shang->xia
        for (int k = i + 1; k < row - i; ++k)
            res.push_back(matrix[k][col - i - 1]);
        //you->zuo
        for (int l = col - i - 2; (l >= i) && (row - i - 1 != i); --l)
            res.push_back(matrix[row - i - 1][l]);
        //xia->shang
        for (int m = row - i - 2; (m > i) && (col - i - 1 != i); --m)
            res.push_back(matrix[m][i]);
    }
    return res;
}

你可能感兴趣的:(剑指offer)