Skip to content

Latest commit

 

History

History
61 lines (49 loc) · 1.47 KB

spirally-traversing-a-matrix.md

File metadata and controls

61 lines (49 loc) · 1.47 KB

Spirally traversing a matrix

Problem Link

Given a matrix of size r*c. Traverse the matrix in spiral form.

Sample Input

4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Sample Output

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 

Solution

class Solution {   
    public: 
    vector<int> spirallyTraverse(vector<vector<int> > M, int r, int c) {
        int limit = r*c;
        vector<int> ans(limit);
        
        int x = 0, y = 0, dx = 1, dy = 0, boundary = 0;
        int index = 0;
        
        while(index < limit) {
            ans[index++] = M[y][x];

            if(dx == 1 && x == c - boundary - 1) {
                dx = 0;
                dy = 1;
            }

            if(dy == 1 && y == r - boundary - 1) {
                dx = -1;
                dy = 0;
            }

            if(dx == -1 && x == boundary) {
                dx = 0;
                dy = -1;
            }

            if(dy == -1 && y == boundary + 1) {
                dx = 1;
                dy = 0;
                ++boundary;
            }

            x += dx;
            y += dy;
        }
        return ans;
    }
};

Accepted

image