Thursday, July 26, 2012

1.6 rotateMatrix

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place?

So the only way I can think of doing it "in place" is if you save one spot. Let me explain. If you have "ABCD" and you want to shift everything forward ONE...you save the "D', shift it to "_ABC", then insert the D. Except this is one a one dimensional array, it's a two dimensional one.

We do this in layers. Say have we have a 4x4 matrix, first we start at "layer 1", where its just the outside border. Then we do layer "2", where its the inside 4 blocks. Then we're done. So we only do N/2.

Have not doublechecked this code yet.

No comments:

Post a Comment