顺时针打印矩阵
vector<int>clockwisePrint(vector<vector<int>>mat,intn,intm){vector<int>ret;inti=0;intj=0;intstartx=0;intstarty=0;intendx=n-1;intendy=m-1;while(startx<=endx&&starty<=endy){if(endy==starty){for(j=endy,i=startx;i<=endx;++i){ret.push_back(mat[i][j]);}returnret;}if(endx==startx){for(i=startx,j=starty;j<=endy;++j){ret.push_back(mat[i][j]);}returnret;}for(i=startx,j=starty;j<=endy;++j){ret.push_back(mat[i][j]);}for(j=endy,i=startx+1;i<=endx;++i){ret.push_back(mat[i][j]);}for(i=endx,j=endy-1;j>=starty;--j){ret.push_back(mat[i][j]);}for(j=starty,i=endx-1;i>startx;--i){ret.push_back(mat[i][j]);}startx++;starty++;endx--;endy--;}returnret;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。