颜色分类(Java)

该博客介绍了如何通过哈希映射和桶排序算法对包含红绿蓝三种颜色的数组进行排序,确保相同颜色的元素相邻并按特定顺序排列。提供了两个解决方案,一个是基于固定颜色映射的哈希映射方法,另一个是适用于可变颜色映射的桶排序实现。这两个方法都在Java中实现,并给出了详细的代码示例和输入输出案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个包含红色、绿色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、绿色、蓝色顺序排列。
此题中,我们使用整数 0、 1 和 2 分别表示红色、绿色和蓝色。

示例 1:
输入:nums = [2,0,2,1,1,0]
输出:[0,0,1,1,2,2]

示例 2:
输入:nums = [2,0,1]
输出:[0,1,2]

示例 3:
输入:nums = [0]
输出:[0]

示例 4:
输入:nums = [1]
输出:[1]

提示:
    n == nums.length
    1 <= n <= 300
    nums[i] 为 0、1 或 2

package com.loo;

import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;

public class SortColor {

    public static void main(String[] args) {
        int[] nums0 = new int[] {2, 0, 2, 1, 1, 0};
        int[] match0 = new int[] {0, 1 , 2};
        int[] nums = new int[] {2, 0, 2, 5 , 3, 1, 4, 1, 0 , 7, 4 , 6 , 1 , 3};
        int[] match = new int[] {0, 1 , 2 ,3 , 4 , 5 , 6 , 7};
        System.out.println(Arrays.toString(getSortColor(nums0 , match0)));
        System.out.println(Arrays.toString(getSortColor1(nums , match)));
    }

    // 对已知少量的 match 可以这样算,但如果考虑到 match 可变,这里适应性比较差
    public static int[] getSortColor(int[] arr , int[] match) {
        if (arr == null || arr.length <= 1 || 
                match == null || match.length == 0) {
            return arr;
        }
        int N = arr.length;
        Map<Integer , Integer> map0 = new HashMap<Integer , Integer>();
        Map<Integer , Integer> map1 = new HashMap<Integer , Integer>();
        Map<Integer , Integer> map2 = new HashMap<Integer , Integer>();
        for (int i=0;i<N;i++) {
            if (arr[i] == match[0]) {
                map0.put(i , match[0]);
            } else if (arr[i] == match[1]) {
                map1.put(i , match[1]);
            } else if (arr[i] == match[2]) {
                map2.put(i , match[2]);
            }
        }
        int[] result = new int[N];
        int index = 0;
        for (int key : map0.keySet()) {
            result[index++] = map0.get(key);
        }
        for (int key : map1.keySet()) {
            result[index++] = map1.get(key);
        }
        for (int key : map2.keySet()) {
            result[index++] = map2.get(key);
        }
        return result;
    }

    // getSortColor1 的 match 自适应可变长度
    public static int[] getSortColor1(int[] arr , int[] match) {
        if (arr == null || arr.length <= 1 || 
                match == null || match.length == 0) {
            return arr;
        }
        int N = arr.length;
        // 相当于桶排序,这里 count 只作统计 ,计算在 arr 中分别有 
        // match[0 --- match.length-1] 多少个,对应记录个数在
        // count[0 --- match.length-1] 中
        int[] count = new int[match.length];
        for (int i=0;i<N;i++) {
            int index = getIndexNum(match , arr[i]);
            if (index != -1 && index < count.length) {
                count[index]++;
            }
        }
        int[] result = new int[arr.length];
        int index = 0;
        for (int i=0;i<count.length;i++) {
            if (count[i] > 0) {
                for (int j=0;j<count[i];j++) {
                    result[index++] = match[i];
                }
            }
        }
        return result;
    }

    public static int getIndexNum(int[] match , int index) {
        if (match == null || match.length == 0) {
            return -1;
        }
        for (int i=0;i<match.length;i++) {
            if (index == match[i]) {
                return i;
            }
        }
        return -1;
    }
                                                                                                                                                                                                            
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值