6-7 lines c++


  • 0
    L
    vector<int> spiralOrder(vector<vector<int>>& g) {
        int m = g.size(), n = m ? g[0].size() : 0, d = 0, i = 0, j = -1;
        vector<int> res, dir = {0,1,0,-1,0}, step = {n+1, m};
        while(--step[d%2]){
            for(int k=0; k<step[d%2]; ++k) res.push_back(g[i+=dir[d]][j+=dir[d+1]]);
            d = ++d%4;
        }
        return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.