循环数组打印

来源:互联网 发布:淘宝一年可以扣多少分 编辑:程序博客网 时间:2024/06/11 02:39
class Printer {public:    vector<int> clockwisePrint(vector<vector<int> > mat, int n, int m) {        // write code here        vector<int> buf;        if(mat.empty())            return buf;        int st_x=0;        int end_x=n-1;        int st_y=0;        int end_y=m-1;       int i=0;        int j=0;        while(st_x<=end_x&&st_y<=end_y){            if(st_x==end_x){                for(;j<=end_y;j++)                    buf.push_back(mat[i][j]);                return buf;            }            if(st_y==end_y){                for(;i<=end_x;i++)                    buf.push_back(mat[i][j]);                return buf;            }            //第一行            for(;j<end_y;j++)                buf.push_back(mat[i][j]);            //第一列            for(;i<end_x;i++)                buf.push_back(mat[i][j]);            //第二行            for(;j>st_y;j--)                buf.push_back(mat[i][j]);            //第二列            for(;i>st_x;i--)                buf.push_back(mat[i][j]);            i++;            j++;            st_x++;            st_y++;            end_x--;            end_y--;        }        return buf;    }};

0 0
原创粉丝点击