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.
My solution using C++. It's not the best or the most efficient take so i'll try to look for a better approach.
voidmatrixRotation(std::vector<std::vector<int>>matrix,intr){std::vector<std::vector<int>>res=matrix;/* Max number of iterations needed to rotate every layer in the 2d vector */uint16_tmaxIterations=std::min(matrix.size(),matrix[0].size())/2;for(intiteration=0;iteration<maxIterations;iteration++){/* Limit of looping through each layer */int32_trow=matrix.size()-2*iteration,rowIterator=iteration;int32_tcol=matrix[0].size()-2*iteration,colIterator=iteration;intperimeter=(2*(row+col)-4),tempRot=r;r=r%perimeter;// Removing redundant rotationsintloopAround=0;/* Top side of the current layer */while(loopAround<r&&colIterator<col+iteration){loopAround++,colIterator++;}if(colIterator-iteration>=col){loopAround--,colIterator--;}/* Right side of the current layer */while(loopAround<r&&rowIterator<row+iteration){loopAround++,rowIterator++;}if(rowIterator-iteration>=row){loopAround--,rowIterator--;}/* Bottom side of the current layer */while(loopAround<r&&colIterator>=iteration){loopAround++,colIterator--;}if(colIterator<iteration){loopAround--,colIterator++;}/* Left side of the current layer */while(loopAround<r&&rowIterator>=iteration){loopAround++,rowIterator--;}if(rowIterator<iteration){loopAround--,rowIterator++;}/* Now we have the location of the element to start the rotation from at matrix[rowIterator][colIterator] *//* Top side of the current layer except last element */for(intlayerCol=iteration;layerCol<col+iteration-1;layerCol++){res[iteration][layerCol]=matrix[rowIterator][colIterator];if(rowIterator==iteration&&colIterator<col+iteration-1&&colIterator>=iteration){colIterator++;}elseif(colIterator==col+iteration-1&&rowIterator<row+iteration-1&&rowIterator>=iteration){rowIterator++;}elseif(rowIterator==row+iteration-1&&colIterator<=col+iteration-1&&colIterator>iteration){colIterator--;}elseif(colIterator==iteration&&rowIterator<=row+iteration-1&&rowIterator>iteration){rowIterator--;}}/* Right side of the current layer except last element */for(intlayerRow=iteration;layerRow<row+iteration-1;layerRow++){res[layerRow][col+iteration-1]=matrix[rowIterator][colIterator];if(rowIterator==iteration&&colIterator<col+iteration-1&&colIterator>=iteration){colIterator++;}elseif(colIterator==col+iteration-1&&rowIterator<row+iteration-1&&rowIterator>=iteration){rowIterator++;}elseif(rowIterator==row+iteration-1&&colIterator<=col+iteration-1&&colIterator>iteration){colIterator--;}elseif(colIterator==iteration&&rowIterator<=row+iteration-1&&rowIterator>iteration){rowIterator--;}}/* Bottom side of the current layer except last element */for(intlayerCol=col+iteration-1;layerCol>iteration;layerCol--){res[row+iteration-1][layerCol]=matrix[rowIterator][colIterator];if(rowIterator==iteration&&colIterator<col+iteration-1&&colIterator>=iteration){colIterator++;}elseif(colIterator==col+iteration-1&&rowIterator<row+iteration-1&&rowIterator>=iteration){rowIterator++;}elseif(rowIterator==row+iteration-1&&colIterator<=col+iteration-1&&colIterator>iteration){colIterator--;}elseif(colIterator==iteration&&rowIterator<=row+iteration-1&&rowIterator>iteration){rowIterator--;}}/* Left side of the current layer except last element */for(intlayerRow=row+iteration-1;layerRow>iteration;layerRow--){res[layerRow][iteration]=matrix[rowIterator][colIterator];if(rowIterator==iteration&&colIterator<col+iteration-1&&colIterator>=iteration){colIterator++;}elseif(colIterator==col+iteration-1&&rowIterator<row+iteration-1&&rowIterator>=iteration){rowIterator++;}elseif(rowIterator==row+iteration-1&&colIterator<=col+iteration-1&&colIterator>iteration){colIterator--;}elseif(colIterator==iteration&&rowIterator<=row+iteration-1&&rowIterator>iteration){rowIterator--;}}r=tempRot;}for(inti=0;i<matrix.size();i++){for(intj=0;j<matrix[0].size();j++){std::cout<<matrix[i][j]<<" ";}std::cout<<std::endl;}std::cout<<std::endl;for(inti=0;i<res.size();i++){for(intj=0;j<res[0].size();j++){std::cout<<res[i][j]<<" ";}std::cout<<std::endl;}}
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 →
My solution using C++. It's not the best or the most efficient take so i'll try to look for a better approach.