旋转数组
解题思路:环形旋转
class Solution { public void rotate(int[] nums, int k) { int len = nums.length; k = k%len; int count = 0; int temp = 0; int start = 0; int i = 0; int swp = 0; while(count<len){ i=start; temp = nums[i]; do{ swp = nums[(i+k)%len]; nums[(i+k)%len] = temp; temp = swp; ++count; i=(i+k)%len; } while(i!=start); ++start; } } }
原文地址:https://www.cnblogs.com/erdanyang/p/11497292.html
时间: 2024-11-06 14:02:00