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

牛的品种排序II

https://www.nowcoder.com/practice/43e49fbb98b4497ba46e185918188b1c

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param cows int整型一维数组
     * @return int整型一维数组
     */
    public int[] sortCows (int[] cows) {
        // write code here
        int left = 0;
        int right = cows.length - 1;
        while (left < cows.length) {
            while (left < right && cows[left] == 0 ) {
                left++;
            }
            while (left < right && cows[right] != 0) {
                right--;
            }
            if (left < right) {
                int tmp = cows[left];
                cows[left] = cows[right];
                cows[right] = tmp;
            }
            left++;
            right--;
        }
        left = 0;
        right = cows.length - 1;
        while (left < cows.length && cows[left] == 0) {
            left++;
        }
        while (left < cows.length) {
            while (left < right && cows[left] == 1 ) {
                left++;
            }
            while (left < right && cows[right] != 1) {
                right--;
            }
            if (left < right) {
                int tmp = cows[left];
                cows[left] = cows[right];
                cows[right] = tmp;
            }
            left++;
            right--;
        }

        return cows;
    }
}

全部评论

相关推荐

野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
自来熟的放鸽子能手面...:这个不一定,找hr跟进一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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