顺序打印数组
顺时针打印矩阵
http://www.nowcoder.com/questionTerminal/9b4c81a02cd34f76be2659fa0d54342a
思想:
顺序打印:上外圈->右外圈->下外圈->左外圈;
注意/难点:->转弯时不要重复打印以及转弯的条件
vector<int> printMatrix(vector<vector<int>> m) {
int row_begin = 0;
int row_end = m.size() - 1;
int col_begin = 0;
int col_end = m[0].size() - 1;
vector<int> ret;
// if(!row_end || !col_end) return m;
while(row_begin <= row_end && col_begin <= col_end){
if(col_end >= col_begin){
for(int i = col_begin; i <= col_end; ++i){
ret.push_back(m[row_begin][i]);
}
}
if(row_end >= row_begin ){
for(int i = row_begin + 1; i <= row_end; ++i ){
ret.push_back(m[i][col_end]);
}
}
if(col_end > col_begin && row_end > row_begin){
for(int i = col_end-1; i >= col_begin; --i){
ret.push_back(m[row_end][i]);
}
}
if(row_end > row_begin && col_end > col_begin){
for(int i = row_end-1; i > row_begin; --i){
ret.push_back(m[i][col_begin]);
}
}
++row_begin;
--row_end;
++col_begin;
--col_end;
}
return ret;
}</int></int></int>