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.
publicstaticvoidmatrixRotation(List<List<Integer>>matrix,intr){intn=matrix.size();intm=matrix.get(0).size();intmin=Math.min(n,m);for(inti=0;i<min/2;i++){intround=r%((n-2*i)*2+(m-2*i)*2-4);intk=0;while(k<round){intfirst=i;//int last = Math.min(m - i - 1, n - i - 1);intlastN=n-i-1;intlastM=m-i-1;inttemp=matrix.get(first).get(first);for(intj=first;j<lastM;j++){matrix.get(first).set(j,matrix.get(first).get(j+1));}for(intj=first;j<lastN;j++){matrix.get(j).set(lastM,matrix.get(j+1).get(lastM));}for(intj=lastM;j>first;j--){matrix.get(lastN).set(j,matrix.get(lastN).get(j-1));}for(intj=lastN;j>first;j--){matrix.get(j).set(first,matrix.get(j-1).get(first));}matrix.get(first+1).set(first,temp);k++;}}matrix.forEach(row->{StringrowString=row.stream().map(Object::toString).collect(Collectors.joining(" "));System.out.println(rowString);});}}
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 →