牛骨文教育服务平台(让学习变的简单)
博文笔记

循环数组打印

创建时间:2016-05-20 投稿人: 浏览次数:155
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;
    }
};

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。