vector<int> printMatrix(vector<vector<int>> matrix) { int M = matrix[0].size(); int N = matrix.size(); int left=0,right=M-1,top=0,bot=N-1; vector<int> result; while(left<=right && top<=bot) { //to right for(int i=left; i<=right;++i) result.push_back(matrix[top][i]); //to down for(int i=top+1;i<=bot;i++) result.push_back(matrix[i][right]); //to left if(bot!=top) for(int i=right-1;i>=left;--i) result.push_back(matrix[bot][i]); //to up if(left!=right) for(int i=bot-1;i>top;i--) result.push_back(matrix[i][left]); left++; right--;top++;bot--; } return result; }