题解 | #向左移动牛群#
向左移动牛群
https://www.nowcoder.com/practice/e70fc604c3684ce294e6af5e97feff04
所用知识
数组、循环
所用语言
java
解题思路
双重循环,每一次把第一个值放置到最后,其他值前移一位。
完整代码
public int[] rotateCows (int[] nums, int k) {
// write code here
int n = nums.length;
k=k%n;
int tem=0;
for(int i=0;i<n-k;i++){
tem=nums[0];
for(int j=0;j<n-1;j++){
nums[j]=nums[j+1];
}
nums[n-1]=tem;
}
return nums;
}
#向左移动牛群#