Rotate Array

189. Rotate Array


requirement: 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 you can, there are at least 3 different ways to solve this problme.


strategy 1:
using arraycopy(Object src, int srcPos, Object dest, int destPos, int length) from the Java APIlibrary
1. first create a new array called newList, let k= length, n = src.length
2. copy the subarray in src from srcPos(n-k) to srcPos(n-k) + k-1 substring into the new array at the position from dest(0) to dest(0) + k -1 
3. then copy the subarray in src from srcPos(0) to srcPos(k)+n-k-1 substring into the new array position from dest(k) to dest(k) + n-k -1
4. then copy the whole new string into the original string 


public class Solution{
   public void rotate(int[] nums, int k){
       int n = nums.length;
       int[] newList = new int[n];
       if (n > 1){
          k = k % n;
          System.arraycopy(nums, n-k, newList, 0, k);
          System.arraycopy(nums, 0, newList, k, n-k);
          System.arraycopy(newList, 0, nums, 0, n);
       }
   }
}


strategy 2:
1. first reverse the substring (0, n-k-1)
2. then reverse the substring (n-k, n-1)
3. finally reverse the whole string and return the new string
total space O(1)
public class Solution{
  public void rotate(int[] nums, int k){
      if(nums == null || nums.length < 2){
         return;
      }
      k = k % nums.length;
      reverse(nums, 0, nums.length-k-1);
      reverse(nums, nums.length-k, nums.length-1);
      reverse(nums, 0, nums.length-1);
   }
   private void reverse(int[] nums, int i, int j){
       int tmp = 0;
       while(i<j){
          tmp = nums[i];
          nums[i] = nums[j];
          nums[j] = tmp;
          i++;
          j--;
       }
    }
}


strategy 3
1. using stack to store the substring (n-k, n-1)
2. then replace array nums[i+k] = nums[i] from n-k-1 to 0
3. then assign substring (0, k-1) with the stack element using pop() function
public class Solution{
  public void rotate(int[] nums, int k){
      if(nums == null || nums.length < 2){
         return;
      }
      k = k % nums.length;
      Stack<Integer> stack = new Stack<Integer>();
      for(int i= nums.length-1; i>= nums.length - k; i--){
          stack.push(nums[i]);
      }
      for(int i= nums.length-k-1; i>=0; i--){
            nums[i+k]=nums[i];
      }
      for(int i=0; i<k; i++){
         nums[i]=stack.pop();
      }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值