My three way to solve this problem, the first way is interesting(JAVA)
Method 1: ( Interesting way, O(n) time cost, O(1) space cost)\n\n    public class Solution {\n        public void rotate(int[] nums, int k) {\n            if(nu
Method 1: ( Interesting way, O(n) time cost, O(1) space cost)\n\n    public class Solution {\n        public void rotate(int[] nums, int k) {\n            if(nums.length <= 1){\n                return;\n            }\n            //step each time to move\n            int step = k % nums.length;\n            //find GCD between nums length and step\n            int gcd = findGcd(nums.length, step);\n            int position, count;\n            \n            //gcd path to finish movie\n            for(int i = 0; i < gcd; i++){\n                //beginning position of each path\n                position = i;\n                //count is the number we need swap each path\n                count = nums.length / gcd - 1;\n                for(int j = 0; j < count; j++){\n                    position = (position + step) % nums.length;\n                    //swap index value in index i and position\n                    nums[i] ^= nums[position];\n                    nums[position] ^= nums[i];\n                    nums[i] ^= nums[position];\n                }\n            }\n        }\n        \n        public int findGcd(int a, int b){\n            return (a == 0 || b == 0) ? a + b : findGcd(b, a % b);\n        }\n        \n    }\n\nMethod 2:( 3 reverse thinking, O(n) time cost, O(1) space cost)\n\n    public class Solution {\n        public void rotate(int[] nums, int k) {\n            if(nums.length <= 1){\n                return;\n            }\n            //step each time to move\n            int step = k % nums.length;\n            reverse(nums,0,nums.length - 1);\n            reverse(nums,0,step - 1);\n            reverse(nums,step,nums.length - 1);\n        }\n        \n        //reverse int array from n to m\n        public void reverse(int[] nums, int n, int m){\n            while(n < m){\n                nums[n] ^= nums[m];\n                nums[m] ^= nums[n];\n                nums[n] ^= nums[m];\n                n++;\n                m--;\n            }\n        }\n    }\n\nMethod 3:( Normal way, O(n) time cost, O(k % nums.length) space cost)\n\n    public class Solution {\n        public void rotate(int[] nums, int k) {\n            if(nums.length <= 1){\n                return;\n            }\n            //step each time to move\n            int step = k % nums.length;\n            int[] tmp = new int[step];\n            for(int i = 0; i < step; i++){\n                tmp[i] = nums[nums.length - step + i];\n            }\n            for(int i = nums.length - step - 1; i >= 0; i--){\n                nums[i + step] = nums[i];\n            }\n            for(int i = 0; i < step; i++){\n                nums[i] = tmp[i];\n            }\n            \n        }\n        \n    }\n\t\n
['Java']