最小的k个数

题目大致为:
输入n个整数,找出其中最小的k个数。
思路:
使用类似二叉查找树的形式实现,控制好树中的结点个数为k。
Java代码

package com.sun.jojo.datastructure;

import java.util.Iterator;
import java.util.TreeSet;

/**
 * Author sunjiamin
 * Date 2017/8/15 16:10
 * Describe :
 */
public class MinK {
    public static void main(String args[]) {
        // 测试的例子
        int array[] = { 4, 5, 1, 6, 2, 7, 3, 8 };
        final int k = 4;
        TreeSet<Integer> set = getLeastNumbers(array, k);
        // 输出
        Iterator<Integer> it = set.iterator();
        System.out.println("最小的" + k + "个数为:");
        while (it.hasNext()) {
            System.out.print(it.next() + "、");
        }

    }

    /**
     * TreeSet 特性 如果元素具备自然顺序 的特性,那么就按照元素自然顺序的特性进行排序存储。
     *  存放进的数据会自动排好序
     * @param array
     * @param k
     * @return
     */
    public static TreeSet<Integer> getLeastNumbers(int array[], int k) {
        TreeSet<Integer> set = new TreeSet<Integer>();
        // 判断k和array的合法性
        if (array == null || k <= 0) {
            return null;
        }

        for (int i = 0; i < array.length; i++) {
            if (set.size() < k) {// 如果TreeSet中的元素小于K个,则直接插入
                set.add(array[i]);
            } else {// TreeSet中的元素大于K个
                if (set.last() > array[i]) {// 最大的元素大于array[i]
                    set.pollLast();// 移除
                    set.add(array[i]);// 加入新的
                }
            }
        }

        return set;

    }
}

结果:

最小的4个数为:
1、2、3、4、

这里稍微看看TreeSet的add内部实现:

public V put(K key, V value) {
        Entry<K,V> t = root;
        if (t == null) {
            compare(key, key); // type (and possibly null) check

            root = new Entry<>(key, value, null);
            size = 1;
            modCount++;
            return null;
        }
        int cmp;
        Entry<K,V> parent;
        // split comparator and comparable paths
        Comparator<? super K> cpr = comparator;
        if (cpr != null) {
            do {
                parent = t;
                cmp = cpr.compare(key, t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        else {
            if (key == null)
                throw new NullPointerException();
            @SuppressWarnings("unchecked")
                Comparable<? super K> k = (Comparable<? super K>) key;
            do {
                parent = t;
                cmp = k.compareTo(t.key);
                if (cmp < 0)
                    t = t.left;
                else if (cmp > 0)
                    t = t.right;
                else
                    return t.setValue(value);
            } while (t != null);
        }
        Entry<K,V> e = new Entry<>(key, value, parent);
        if (cmp < 0)
            parent.left = e;
        else
            parent.right = e;
        fixAfterInsertion(e);
        size++;
        modCount++;
        return null;
    }

调用了cpr.compare(key, t.key);进行排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值