题解 | #向左移动牛群#
向左移动牛群
https://www.nowcoder.com/practice/e70fc604c3684ce294e6af5e97feff04
#include <vector>
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 len = nums.size();
k %= len;
for(int i=0; i<k; ++i)
{
int temp = nums[len-1];
for(int j=len-1; j>0; --j)
{
nums[j] = nums[j-1];
}
nums[0] = temp;
}
return nums;
}
};
虚数五行区解题中心 文章被收录于专栏
非淡泊无以明志,非宁静无以致远


查看21道真题和解析