首先,最容易想到的就是横着向右打印,即代码中

   for(int i = col ; i <= cols ; ++i){
        result.push_back(matrix[row][i]);                    
   }   

向下打印时要确保行数大于2,即 if(row < rows) ,(其中row+1 = rows 即正好为两行,这里需要自己理解!)
向左打印要确保列数大于2,行数大于2 if(col < cols && row < rows)
向上打印要确保行数大于3,列数大于2 if(row + 1 < rows && cols > 0)

这里还需要强调的是循环! 即row和col的作用。

还需要注意vector<vector<int>> 的行、列判断为:
int rows = matrix.size() ; //行数
int cols = matrix[0].size() ; //列数</int>

class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        int rows = matrix.size() ;  //行数
        int cols = matrix[0].size() ; //列数
        if(rows == 1 && cols == 1) return matrix[0];        
        vector<int> result ;
        rows -- ; cols -- ;        
        int row = 0 , col = 0 ;
        while(row <= rows  && col <= cols ){
            cout<<"rows: "<<rows<<" "<<"cols: "<<cols<<endl;
            cout<<"row: "<<row<<" "<<"col: "<<col<<endl;
            for(int i = col ; i <= cols ; ++i){
                result.push_back(matrix[row][i]);                    
            }            
            if(row < rows){  // 向下打印 至少两行 
                for(int i = row + 1 ; i <= rows ; ++i){
                    result.push_back(matrix[i][cols]);
                }
            }            
            if(col < cols && row < rows){//可以横着向左打印  至少两列 
                for(int i = cols-1 ; i >= col ; --i ){
                    result.push_back(matrix[rows][i]);
                }
            }            
            if(row + 1 < rows && cols > 0){//可以竖着向上打印  至少三行
                for(int i = rows -1 ; i > row ; --i){
                    result.push_back(matrix[i][col]);
                }
            }
            rows -- ;
            cols -- ;
            row ++ ;
            col ++ ;                

        }        
        return result ;

    }
};