Java基础-对任意类型实现自定义比较

// leetcode 179.
class Solution {
    // 使用Comparator比较器(自定义比较器)
    Comparator comparator1 = (o1, o2) -> {
        if (o1 instanceof Integer && o2 instanceof Integer) {
            Integer n1 = (Integer) o1;
            Integer n2 = (Integer) o2;

            String s1 = "" + n1 + n2;
            String s2 = "" + n2 + n1;
            return -s1.compareTo(s2);
        }
        return 0;
    };

    // 实现自定义排序的优先队列
    PriorityQueue<Integer> que = new PriorityQueue<Integer>(comparator1);

    public String largestNumber(int[] nums) {
        int len = nums.length;
        if (len == 1) return Integer.toString(nums[0]);

        StringBuilder res = new StringBuilder();

        for (int i = 0; i < len; i++) {
            que.add(nums[i]);
        }

        while (!que.isEmpty()) {
            res.append ("" + que.peek());
            que.poll();
        }

        // 判断是否全是0
        int l = 0;
        while (l < len && res.charAt(l) == '0') { l++; }
        if (l == len) { return "0"; }

        return res.toString();
    }


    // 使用自定义排序的另一种写法
    class Solution {
        // Java的重载
        public String largestNumber(int[] nums, int len) {
            // 创建包装类,方便传入Comparator
            Integer[] arr = new Integer[len];
            for (int i = 0; i < len; i++) {
                arr[i] = nums[i];
            }

            // 自定义比较器的匿名对象
            Arrays.sort(arr, (o1, o2) -> {
                String s1 = "" + o1 + o2;
                String s2 = "" + o2 + o1;
                return -s1.compareTo(s2);
            });

            if (arr[0] == 0) {
                return "0";
            }
            StringBuilder ret = new StringBuilder();
            for (int num : arr) {
                ret.append(num);
            }
            return ret.toString();
        }
    }
}
// leetcode 179.
class Solution {
    // 使用Comparator比较器(自定义比较器)
    Comparator comparator1 = (o1, o2) -> {
        if (o1 instanceof Integer && o2 instanceof Integer) {
            Integer n1 = (Integer) o1;
            Integer n2 = (Integer) o2;

            String s1 = "" + n1 + n2;
            String s2 = "" + n2 + n1;
            return -s1.compareTo(s2);
        }
        return 0;
    };

    // 实现自定义排序的优先队列
    PriorityQueue<Integer> que = new PriorityQueue<Integer>(comparator1);

    public String largestNumber(int[] nums) {
        int len = nums.length;
        if (len == 1) return Integer.toString(nums[0]);

        StringBuilder res = new StringBuilder();

        for (int i = 0; i < len; i++) {
            que.add(nums[i]);
        }

        while (!que.isEmpty()) {
            res.append ("" + que.peek());
            que.poll();
        }

        // 判断是否全是0
        int l = 0;
        while (l < len && res.charAt(l) == '0') { l++; }
        if (l == len) { return "0"; }

        return res.toString();
    }


    // 使用自定义排序的另一种写法
    class Solution {
        // Java的重载
        public String largestNumber(int[] nums, int len) {
            // 创建包装类,方便传入Comparator
            Integer[] arr = new Integer[len];
            for (int i = 0; i < len; i++) {
                arr[i] = nums[i];
            }

            // 自定义比较器的匿名对象
            Arrays.sort(arr, (o1, o2) -> {
                String s1 = "" + o1 + o2;
                String s2 = "" + o2 + o1;
                return -s1.compareTo(s2);
            });

            if (arr[0] == 0) {
                return "0";
            }
            StringBuilder ret = new StringBuilder();
            for (int num : arr) {
                ret.append(num);
            }
            return ret.toString();
        }
    }
}

 

posted @ 2021-07-06 15:34  Peterxiazhen  阅读(147)  评论(0编辑  收藏  举报