剑指Offer(Java实现):数组中出现次数超过一半的数字、最小的k个数

package com.dengzm.jianzhioffer;

import java.util.Random;

/**
 * @Description 039 数组中出现次数超过一半的数字
 * 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字
 *
 * Created by deng on 2019/5/25.
 */
public class Jianzhi039 {



    public static void main(String[] args) {
        int[] data1 = new int[] {1,2,3,4,5,6,6,6,6,6};
        int[] data2 = new int[] {1,2,3,4,5,6,7,8,9,1,1,1,1,1,1,1,1};
        int[] data3 = new int[] {1,2,3,4,5,6,6,6,6};

        System.out.println(getMoreThanHalfUsingPartition(data1));
        System.out.println(getMoreThanHalfUsingPartition(data2));
        System.out.println(getMoreThanHalfUsingPartition(data3));

        System.out.println(getMoreThanHalfUsingCount(data1));
        System.out.println(getMoreThanHalfUsingCount(data2));
        System.out.println(getMoreThanHalfUsingCount(data3));
    }

    /**
     * 方法一:使用快排的方式,寻找数组的中位数
     * 当数组中超过一半的数字为相同数字时,该数组的中位数一定是该数字
     */
    private static int getMoreThanHalfUsingPartition(int[] source) {
        if (source == null || source.length == 0) {
            return -1;
        }

        int middle = source.length / 2;
        int start = 0;
        int end = source.length - 1;
        int index = partition(source, start, end);
        while (index != middle) {
            if (index > middle) {
                end = index - 1;
                index = partition(source, start, end);
            } else {
                start = index + 1;
                index = partition(source, start, end);
            }
        }

        int result = source[index];
        return checkMoreThanHalf(source, result) ? result : -1;
    }

    private static int partition(int[] data, int start, int end) {
        if (data == null || data.length == 0 || end >= data.length) {
            throw new RuntimeException("partition out of range!");
        }

        int index = new Random().nextInt(end - start + 1) + start;
        swap(data, index, end);

        int small = start - 1;
        for (index = start; index <= end; index ++) {
            if (data[index] < data[end]) {
                small ++;
                if (small != index) {
                    swap(data, index, small);
                }
            }
        }

        small ++;
        swap(data, end, small);

        return small;
    }

    private static void swap(int[] data, int left, int right) {
        int temp = data[left];
        data[left] = data[right];
        data[right] = temp;
    }

    /**
     * 方法二:通过遍历数组统计个数找出超过一半的数字
     * 当后一个数与前一个数相同时,计数+1;不同时,计数-1;如果数字为0,则保存下一个数,数字置为1
     * 如果数组中某个数超过一半的数量,则遍历后统计的为该数
     */
    private static int getMoreThanHalfUsingCount(int[] source) {
        if (source == null || source.length == 0) {
            return -1;
        }

        if (source.length == 1) {
            return source[0];
        }

        int result = source[0];
        int times = 1;

        for (int i = 1; i < source.length; i ++) {
            if (times == 0) {
                result = source[i];
                times = 1;
            } else if (result == source[i]) {
                times ++;
            } else {
                times --;
            }
        }

        return checkMoreThanHalf(source, result) ? result : -1;
    }

    private static boolean checkMoreThanHalf(int[] data, int target) {
        int times = 0;
        for (int aData : data) {
            if (aData == target) {
                times++;
            }
        }

        return times * 2 >= data.length;
    }
}

package com.dengzm.jianzhioffer;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Description 040 最小的k个数
 * 输入n个整数,找出其中最小的k个数
 *
 * Created by deng on 2019/9/18.
 */
public class Jianzhi040 {

    public static void main(String[] args) {
        int[] data1 = new int[] {1,2,3,4,5,6,7,8,9,1,1,1,1,1,1,1,1};
        int[] data2 = new int[] {1,2,3,4,5,6,6,6,6};

        getLeastNumbers(data1, 4);
        getLeastNumbers(data2, 4);
    }

    /**
     * 使用最大堆或者红黑树,选出k个数后,后续的数字与最大值进行比较、替换、排序,最后得出最小的k个数
     *
     * 书中使用的是该方法,但是该方法只针对不含重复数字的数组有效,因为Set不可以保存重复的数字
     * 故上面的两个测试项,第一个的结果是不对的。
     * 请知悉
     */
    private static void getLeastNumbers(int[] source, int k) {
        if (source == null || source.length == 0 || k < 1 || source.length < k) {
            System.out.println("data invalid");
            return;
        }

        TreeSet<Integer> result = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        for (int i = 0; i < source.length; i ++) {
            if (result.size() < k) {
                result.add(source[i]);
            } else {
                int max = result.iterator().next();
                if (max > source[i]) {
                    result.remove(max);
                    result.add(source[i]);
                }
            }
        }

        for (int integer : result) {
            System.out.println(integer);
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值