Rotate Array

class Solution {
public:
    void rotate(int nums[], int n, int k) {
        k%=n;
        reverse(nums,0,n-k-1);
        reverse(nums,n-k,n-1);
        reverse(nums,0,n-1);
        
    }
    void reverse(int nums[],int first,int end){
        
            int i=first,j=end;
        for(;i<j;i++,j--){
            nums[i]^=nums[j];
            nums[j]^=nums[i];
            nums[i]^=nums[j];
            
        }    
            
            
        
        
    }
};

 

posted @ 2015-04-30 21:49  ~每天进步一点点~  阅读(90)  评论(0编辑  收藏  举报