package com.leetcode.aaront;

/**
 * @author tonyhui
 * @since 16/3/20
 */
public class RotateArray {
    public static void main(String[] args) {

        int[] nums = {1,2,3,4,5,6,7};
        rotate(nums, 3);
        for(int i = 0;i<nums.length;i++){
            System.out.println(nums[i]);
        }
    }

    /**
     * 效率较低,不能ac
     * @param nums
     * @param k
     */

    public static void rotate(int[] nums, int k) {
        int step=k%nums.length;
        for (int i = 0; i <= step; i++) {
            int temp = nums[0];
            move(nums);
            nums[nums.length-1] = temp;
        }
    }

    public static void move(int[] nums) {
        for (int i = 0, len = nums.length; i < len - 1; i++) {
            nums[i]=nums[i+1];
        }
    }
}

@@ -23,16 +23,42 @@
 aront;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * @
@@ -195,18 +195,24 @@
  %7B1,
+ 
 2,
+ 
 3,
+ 
 4,
+ 
 5,
+ 
 6,
+ 
 7%7D;%0A
@@ -248,92 +248,50 @@
     
-for(int i = 0;i%3Cnums.length;i++)%7B%0A            System.out.println(nums%5Bi%5D);%0A        %7D
+System.out.println(Arrays.toString(nums));
 %0A   
@@ -319,16 +319,23 @@
 %E8%BE%83%E4%BD%8E,%E4%B8%8D%E8%83%BDac%0A
+     *%0A
      * @
@@ -441,11 +441,16 @@
 step
-=k%25
+ = k %25 (
 nums
@@ -456,16 +456,17 @@
 s.length
+)
 ;%0A      
@@ -486,17 +486,16 @@
 = 0; i %3C
-=
  step; i
@@ -528,17 +528,31 @@
  = nums%5B
-0
+nums.length - 1
 %5D;%0A     
@@ -591,21 +591,9 @@
 ums%5B
-nums.length-1
+0
 %5D = 
@@ -604,24 +604,25 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%0A    p
@@ -683,79 +683,770 @@
 i = 
-0, len = nums.length; i %3C len
+nums.length - 1; i %3E 0; i--) %7B%0A            nums%5Bi%5D = nums%5Bi - 1%5D;%0A        %7D%0A    %7D%0A%0A    /**%0A     * %E9%AB%98%E6%95%88%E6%96%B9%E6%B3%95 O(n)%0A     * %E6%80%9D%E8%B7%AF:%0A     * 1 2 3 4 5 6 7 8 9 10 / 4%0A     * step 1: 10 9 8 7 6 5 4 3 2 1%0A     * step 2: 7 8 9 10 6 5 4 3 2 1%0A     * step 3: 7 8 9 10 1 2 3 4 5 6%0A     * ooh amazing ! it is perfect algorithm%0A     */%0A%0A    public void rotate2(int%5B%5D nums, int k) %7B%0A        k %25= nums.length;%0A        reverse(nums, 0, nums.length - 1);  // reverse the whole array%0A        reverse(nums, 0, k - 1);  // reverse the first part%0A        reverse(nums, k, nums.length
  - 1
+)
 ; 
-i++) %7B%0A            nums%5Bi%5D=nums%5Bi+1%5D
+ // reverse the second part%0A    %7D%0A%0A    public void reverse(int%5B%5D nums, int l, int r) %7B%0A        while (l %3C r) %7B%0A            int temp = nums%5Bl%5D;%0A            nums%5Bl++%5D = nums%5Br%5D;%0A            nums%5Br--%5D = temp
 ;%0A  

refer Okami algorithm the time complexity is O(n)