The original description is as follows:
Rotate an array of n elements to the right by K steps.
for example, With n = 7 and k = 3 , the Array [1,2,3,4,5,6,7" is rotated To [5,6,7,1,2,3,4] .
Note:
Try to come up as many solutions as can, there is at least 3 different ways to solve this problem.
[Show hint]
Hint:
Could do it in-place with O (1) extra space?
Related Problem:reverse Words in a String II
My answer is as follows:
public class Solution {public void rotate (int[] nums, int k) {if (nums==null| | k==0) {return;} K = K%nums.length;reverse (nums,0,nums.length-1); reverse (nums,0,k-1); reverse (nums,k,nums.length-1);} public void reverse (int[] A, int. start, int end) {while (start<end) {int temp = A[start];a[start] = A[end];a[end] = Temp;s tart++;end--;}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(Leetcode) Rotating an array