MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / spiralMatrixIII

Method spiralMatrixIII

spiralMatrix3.java:2–30  ·  view source on GitHub ↗
(int rows, int cols, int rStart, int cStart)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
3 int directions[][] = {{0,1},{1,0},{0,-1},{-1,0}};
4 int n = rows*cols;
5 int res[][] = new int[n][2];//[[r,c],[r1,c1]...
6 res[0][0] = rStart;
7 res[0][1] = cStart;
8 int count=1;
9 int step=1;
10 int index=0;
11
12 while(count<n){
13 for(int times=0;times<2;times++){
14 int dr = directions[index%4][0];
15 int dc = directions[index%4][1];
16 for(int i=0;i<step;i++){
17 rStart+=dr;
18 cStart+=dc;
19 if(rStart>=0 && rStart<rows && cStart>=0 && cStart<cols){
20 res[count][0] = rStart;
21 res[count][1] = cStart;
22 count++;
23 }
24 }
25 index++;
26 }
27 step++;
28 }
29 return res;
30 }
31}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected