Вопрос:
Может ли алгоритм, приведенный ниже, быть настроен на использование того же массива (который представляет 2D-матрицу) для вращения по часовой стрелке вместо использования второго массива и при этом оставаться на O (n) сложность?
Код:
import java.util.Random;
public class MatrixRotation {
public static void main(String[] args) {
int dimension = 5;
int[] array = generate(dimension);
print(array, dimension);
int[] clockwise = clockwise(array, dimension);
print(clockwise, dimension);
}
//Generate a matrix with random values
private static int[] generate(int dimension) {
Random rand = new Random();
int[] array = new int[dimension * dimension];
for(int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(10);
}
return array;
}
//Rotates the matrix clockwise by calculating where the value's position should be after the rotation
private static int[] clockwise(int[] array, int dimension) {
int[] rotated = new int[array.length];
int baseCount = dimension;
for(int i = 0; i < array.length; i++) {
int remainder = i % dimension;
if(remainder == 0)
baseCount--;
int position = baseCount + (dimension * remainder);
//I suspect I can do some kinda swapping functionality here but am stumped
rotated[position] = array[i];
}
return rotated;
}
//Used to display the matrix
private static void print(int[] array, int dimension) {
for(int i = 0; i < array.length; i++) {
if(i % dimension == 0)
System.out.println();
System.out.print(array[i] + " ");
}
System.out.println();
}
}
Пример вывода:
1 7 4 1 4
2 3 5 2 9
4 3 9 3 1
5 8 7 5 6
3 3 7 2 5
3 5 4 2 1
3 8 3 3 7
7 7 9 5 4
2 5 3 2 1
5 6 1 9 4
Предыстория:
На днях я читал вопрос о поворотах матриц, представленных в одномерном массиве, и решил рассмотреть его. Мне удалось успешно создать алгоритм поворота, рассчитав следующую позицию значения после поворота. В настоящее время я пытаюсь определить, есть ли способ сохранить его как O (n), при этом уменьшая используемое пространство, сохраняя его в том же массиве. Есть мысли о том, как этого добиться?