1. 程式人生 > >劍指offer--26.順時針打印矩陣

劍指offer--26.順時針打印矩陣

item muti ids des print pty for describe esc

1,2,3,4
5,6,7,8
8,10,11,12
13,14,15,16

每次輸出第一行,然後刪除第一行,逆時針旋轉剩下的矩陣。

------------------------------------------------------------------------------------------------------------------------

時間限制:1秒 空間限制:32768K 熱度指數:430225 本題知識點: 數組

題目描述

輸入一個矩陣,按照從外向裏以順時針的順序依次打印出每一個數字,例如,如果輸入如下4 X 4矩陣: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 則依次打印出數字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
class
Solution { public: vector<int> printMatrix(vector<vector<int> > matrix) { if(matrix.empty() || matrix.size() == 1) return matrix[0]; vector<int> ans; while(!matrix.empty()) { for(int x:matrix[0]) { ans.push_back(x); } vector
<vector<int>> cp; for(int i=0; i<matrix[0].size(); i++) { vector<int> tmp; for(int j=1; j<matrix.size(); j++) { tmp.push_back(matrix[j][i]); } cp.push_back(tmp); } reverse(cp.begin(), cp.end()); matrix
= cp; } return ans; } };

劍指offer--26.順時針打印矩陣