题解 | #向左移动牛群#
向左移动牛群
https://www.nowcoder.com/practice/e70fc604c3684ce294e6af5e97feff04
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param k int整型 * @return int整型vector */ vector<int> rotateCows(vector<int>& nums, int k) { // write code here int length = nums.size(); k = k % length; for (int i = 0; i < k/2; i++) { swap(nums[length-k+i], nums[length-i-1]); } for (int i = 0; i < (length-k)/2; i++) { swap(nums[i], nums[length-k-i-1]); } for (int i = 0; i < length/2; i++) { swap(nums[i], nums[length-i-1]); } return nums; } };