⭐️ 题目描述
leetcode链接:轮转数组
⭕️ C代码实现:
/*[1,2,3,4,5,6,7], k = 3 , numsSize = 7思路:1. 逆置 (0) - (numsSize - k - 1) 下标的元素- [4 , 3 , 2 , 1 , 5 , 6 , 7]2. 逆置 (numsSize - k) - (numsSize - 1) 下标的元素- [3 , 2 , 1 , 7 , 6 , 5 , 4]3. 整体逆置 (0) - (numsSize - 1)- [4 , 5 , 6 , 7 , 1 , 2 , 3]
*/
void reverse (int * range , int left , int right) {while (left < right) {int temp = range[left];range[left] = range[right];range[right] = temp;left++;right--;}
}void rotate(int* nums, int numsSize, int k){// 防止下面操作数组逆置越界k %= numsSize;reverse(nums , 0 , numsSize - k - 1);reverse(nums , numsSize - k , numsSize - 1);reverse(nums , 0 , numsSize - 1);
}