Pregunta de entrevista de Amazon

Give a 2D rectangular array represented as a 1D arrary in row-major form, rotate the array by 90 degrees

Respuestas de entrevistas

Anónimo

20 oct 2011

I came up with a solution where you transpose the array and then exchange the colums. But this is very inefficient. Does anyone have a better solution.

Anónimo

21 oct 2011

Let n be the number of rows and m be the number of columns. Make m stacks of (n-1) size,

Anónimo

21 oct 2011

Let n be the number of rows and m be the number of columns. Make m stacks of (n-1) size and iterate through your "2D" array by successively pushing entries through stacks: eg. (entry i is being pushed in stack ~(i mod m)) when i reaches m x (n-1) pop stack and overwrite entries in the "2D" array from the beginning until empty + i'th entry then go to the next stack. Repeat until done. this should run in m x (n-1) + m x n = m x (2n +1) and uses m x (n-1) x (data entry mem size) extra memory during the run. If you want to use less memory, it gets a whole lot more complicated but won't be as quick.

Anónimo

12 ene 2012

C uses row-major order to lay out arrays in linear order. So: int orig[2][5] = { {0,1,2,3,4}, {5,6,7,8,9} }; is this in memory: 0,1,2,3,4,5,6,7,8,9 Assuming clockwise rotation, I think we want to transform: 56789 01234 into: 94 83 72 61 50 which is equivalent to a transpose then a mirror about col==nrows. Assuming this is so: #include const int nrows = 2; const int ncols = 5; int orig[2][5] = { {0,1,2,3,4}, {5,6,7,8,9} }; int final[5][2]; void main() { int row, col; for(row=0;row