题解 | #牛的品种排序I#

牛的品种排序I

https://www.nowcoder.com/practice/e3864ed7689d460c9e2da77e1c866dce

所用知识

数组、循环遍历

所用语言

java

解题思路

每当循环到一个零,数组从左到右就添加一个零 再把剩下的的的位置都改为一

完整代码

public int[] sortCows (int[] cows) {
    // write code here
    int tem =0;
    for(int i=0;i<cows.length;i++){
        if(cows[i]==0){
            cows[tem]=0;
            tem++;
        }
    }
    while(tem<cows.length){
        cows[tem]=1;
        tem++;
    }
    return cows;
}
#牛的品种排序I#
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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