leetcode初级算法- 旋转数组

#include 
#include 
#include
void rotate(int* nums, int numsSize, int k) ;
int main()
{
    int nums[]={1,2,3,4,5,6};
    int numsSize=6,k=2;
    rotate(nums,numsSize, k) ;
    return 0;
}
void rotate(int* nums, int numsSize, int k) {
    int i=numsSize,k2=0,temp1=nums[0],temp2,index=0;
    while(i>0)
    {
        k2=(k2+k)%numsSize;
        temp2=nums[k2];
        nums[k2]=temp1;
        temp1=temp2;
        if(index==k2)  //当元素陷入局部闭环时跳出
        {
            index=(index+1)%numsSize;
            k2=index;
            temp1=nums[index];
        }
        i--;
    }
    for(i=0;i

你可能感兴趣的:(leetcode初级算法- 旋转数组)