/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem54;

/**
 * Given an image represented by an NxN matrix, where each pixel in the image is an integer, write
 * a method to rotate the image by 90 degrees. Can you do this in place?
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class RotateMatrix {

}

@@ -897,10 +897,1894 @@
 trix %7B%0A%0A
+  /**%0A   * Iterative algorithm to solve this problem. This solution is based on move 4 positions per%0A   * iteration. With this approach we can reduce the complexity order in space terms from O(N%5E2) to%0A   * O(1). In time terms, the complexity order is O(N%5E2) where N is the height or width of the%0A   * matrix.%0A   */%0A  public void rotate(int%5B%5D%5B%5D matrix) %7B%0A    validateInput(matrix);%0A%0A    int length = matrix.length;%0A    for (int i = 0; i %3C length / 2; ++i) %7B%0A      int first = i;%0A      int last = length - 1 - i;%0A      for (int j = first; j %3C last; ++j) %7B%0A        int offset = j - first;%0A        int top = matrix%5Bfirst%5D%5Bj%5D;%0A        moveLeftToTop(matrix, first, last, j, offset);%0A        moveBottomToLeft(matrix, first, last, offset);%0A        moveRithToBottom(matrix, last, j, offset);%0A        moveTopToRight(matrix, last, j, top);%0A      %7D%0A    %7D%0A  %7D%0A%0A  private void moveTopToRight(int%5B%5D%5B%5D matrix, int last, int i, int top) %7B%0A    matrix%5Bi%5D%5Blast%5D = top;%0A  %7D%0A%0A  private void moveRithToBottom(int%5B%5D%5B%5D matrix, int last, int i, int offset) %7B%0A    matrix%5Blast%5D%5Blast - offset%5D = matrix%5Bi%5D%5Blast%5D;%0A  %7D%0A%0A  private void moveBottomToLeft(int%5B%5D%5B%5D matrix, int first, int last, int offset) %7B%0A    matrix%5Blast - offset%5D%5Bfirst%5D = matrix%5Blast%5D%5Blast - offset%5D;%0A  %7D%0A%0A  private void moveLeftToTop(int%5B%5D%5B%5D matrix, int first, int last, int i, int offset) %7B%0A    matrix%5Bfirst%5D%5Bi%5D = matrix%5Blast - offset%5D%5Bfirst%5D;%0A  %7D%0A%0A  private void validateInput(int%5B%5D%5B%5D matrix) %7B%0A    if (matrix == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null instance of matrix as parameter.%22);%0A    %7D else if (matrix.length == 0) %7B%0A      throw new IllegalArgumentException(%0A          %22You can't pass an empty instance of matrix as parameter.%22);%0A    %7D else if (matrix.length != matrix%5B0%5D.length) %7B%0A      throw new IllegalArgumentException(%0A          %22You can't pass a matrix with different height and width.%22);%0A    %7D%0A  %7D%0A
 %7D%0A

Add iterative solution to problem 54