| 24 | |
| 25 | */ |
| 26 | class Solution { |
| 27 | public int[][] generateMatrix(int n) { |
| 28 | int[][] ans=new int[n][n]; |
| 29 | int fr=0,lr=n-1,fc=0,lc=n-1; |
| 30 | int val=1; |
| 31 | while(fr<=lr && fc<=lc) |
| 32 | { |
| 33 | |
| 34 | for(int i=fc;i<=lc;i++) |
| 35 | { |
| 36 | ans[fr][i]=val++; |
| 37 | } |
| 38 | fr++; |
| 39 | if(fr>lr) break; |
| 40 | |
| 41 | for(int i=fr;i<=lr;i++) |
| 42 | { |
| 43 | ans[i][lc]=val++; |
| 44 | } |
| 45 | lc--; |
| 46 | if(lc<fc) break; |
| 47 | |
| 48 | for(int i=lc;i>=fc;i--) |
| 49 | { |
| 50 | ans[lr][i]=val++; |
| 51 | } |
| 52 | lr--; |
| 53 | if(lr<fr) break; |
| 54 | |
| 55 | for(int i=lr;i>=fr;i--) |
| 56 | { |
| 57 | ans[i][fc]=val++; |
| 58 | } |
| 59 | fc++; |
| 60 | } |
| 61 | return ans; |
| 62 | } |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected