题解 | #颜色分类#

颜色分类

https://www.nowcoder.com/practice/52e04ddb7b5640a8869c2d3da2ad3344

import java.util.*;

/**
 * NC212 颜色分类
 * @author d3y1
 */
public class Solution {
    private final int KIND = 3;

    private interface Color{
        int RED = 0;
        int WHITE = 1;
        int BLUE = 2;
    }

    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param colors int整型一维数组
     * @return int整型一维数组
     */
    public int[] sortColor (int[] colors) {
        // return solution1(colors);
        // return solution2(colors);
        // return solution3(colors);
        return solution4(colors);
    }

    /**
     * 统计
     * @param colors
     * @return
     */
    private int[] solution1(int[] colors){
        int n = colors.length;

        int[] cnt = new int[KIND];
        for(int color: colors){
            cnt[color]++;
        }

        // int[] result = new int[n];

        // int i = 0;
        // for(int j=1; j<=cnt[Color.RED]; j++){
        //     result[i++] = Color.RED;
        // }
        // for(int j=1; j<=cnt[Color.WHITE]; j++){
        //     result[i++] = Color.WHITE;
        // }
        // for(int j=1; j<=cnt[Color.BLUE]; j++){
        //     result[i++] = Color.BLUE;
        // }

        // for(int color=Color.RED; color<=Color.BLUE; color++){
        //     for(int j=1; j<=cnt[color]; j++){
        //         colors[i++] = color;
        //     }
        // }

        for(int i=0; i<n; i++){
            if(i < cnt[Color.RED]){
                colors[i] = Color.RED;
            }else if(cnt[Color.RED]<=i && i<cnt[Color.RED]+cnt[Color.WHITE]){
                colors[i] = Color.WHITE;
            }else{
                colors[i] = Color.BLUE;
            }
        }

        return colors;
    }

    /**
     * 数组排序
     * @param colors
     * @return
     */
    private int[] solution2(int[] colors){
        Arrays.sort(colors);

        return colors;
    }

    /**
     * 三指针
     * @param colors
     * @return
     */
    private int[] solution3(int[] colors){
        int n = colors.length;

        int left = 0;
        int right = n-1;
        for(int i=0; i<=right; i++){
            // 2 -> 右边
            // (2...2) -> (2...2)
            // (2...1) -> (1...2)
            // (2...0) -> (0...2)
            while(i<=right && colors[i]==Color.BLUE){
                swap(colors, i, right--);
            }

            // 左边 <- 0
            // (0) <- (0)
            // (0...1) <- (1...0)
            if(colors[i] == Color.RED){
                swap(colors, i, left++);
            }
        }

        return colors;
    }

    /**
     * 三指针: 简化
     * @param colors
     * @return
     */
    private int[] solution4(int[] colors){
        int n = colors.length;

        int left = 0;
        int right = n-1;
        int i = 0;
        while(i <= right){
            // 2 -> 右边
            // (2...2) -> (2...2)
            // (2...1) -> (1...2)
            // (2...0) -> (0...2)
            if(colors[i] == Color.BLUE){
                swap(colors, i, right--);
            }
            // 左边 <- 0
            // (0) <- (0)
            // (0...1) <- (1...0)
            else if(colors[i] == Color.RED){
                swap(colors, i++, left++);
            }
            else{
                i++;
            }
        }

        return colors;
    }

    /**
     * 交换
     * @param colors
     * @param m
     * @param n
     */
    private void swap(int[] colors, int m, int n){
        int tmp = colors[m];
        colors[m] = colors[n];
        colors[n] = tmp;
    }
}

全部评论

相关推荐

抽纸大侠:抱抱😘,首先你还有春招,然后就算这时候没上岸也没关系,大部分人都是这样,毕业了再找也成,最后工作只是生活的一小部分,找到工作也不是一个必须的事情。不要气馁不要焦虑你只是陷入了短暂的低谷,你也一直有退路
点赞 评论 收藏
分享
05-23 19:33
重庆大学 Java
只学了传统后端,马上去后端实习了,在想要不要学习agent开发相关的。27秋招和26相比难度如何?
我连备胎都不是却还在...:就暑期实习而言,大厂官宣hc 比 26 多,但是我观察看应该低于 26 的,估计秋招也不简单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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