We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticvoidmatrixRotation(List<List<int>>matrix,intr){introwsC=matrix.Count;intcolsC=matrix[0].Count;intrm=0;intrM=rowsC-1;intcm=0;intcM=colsC-1;intlength=(cM-cm+1)*2+(rM-rm+1)*2-4;int[]a=newint[length];// Check if there is something to shiftwhile(cm<cM&&rm<rM){intshifts=r%length;// Fill array from matrix part given by min/max row/columnfor(inti=cm;i<=cM;i++){a[i-cm]=matrix[rm][i];a[cM-cm+rM-rm+i-cm]=matrix[rM][cM-i+cm];}for(intj=rm+1;j<=rM-1;j++){a[cM-cm+j-(rm+1)+1]=matrix[j][cM];a[cM-cm+rM-rm+cM-cm+j-rm]=matrix[rM-1+rm+1-j][cm];}// Shift arrayfor(inti=0;i<length/2;i++){(a[i],a[length-1-i])=(a[length-1-i],a[i]);}for(inti=0;i<(length-shifts)/2;i++){(a[i],a[length-shifts-1-i])=(a[length-shifts-1-i],a[i]);}for(inti=0;i<shifts/2;i++){(a[length-shifts+i],a[length-1-i])=(a[length-1-i],a[length-shifts+i]);}// Update matrix part given by min/max row/column from shifted arrayfor(inti=cm;i<=cM;i++){matrix[rm][i]=a[i-cm];matrix[rM][cM-i+cm]=a[cM-cm+rM-rm+i-cm];}for(intj=rm+1;j<=rM-1;j++){matrix[j][cM]=a[cM-cm+j-(rm+1)+1];matrix[rM-1+rm+1-j][cm]=a[cM-cm+rM-rm+cM-cm+j-rm];}// Switch matrix part given by min/max row/column to the next levelrm+=1;rM-=1;cm+=1;cM-=1;length-=8;}foreach(vartinmatrix){foreach(vart1int)Console.Write($"{t1} ");Console.WriteLine();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
C#