题解 | #向左移动牛群#

向左移动牛群

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;

}
#向左移动牛群#
全部评论

相关推荐

点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务