《程序员代码面试指南》第八章 数组和矩阵问题 在数组中找到出现次数大于N/K 的数

题目

在数组中找到出现次数大于N/K 的数

java代码

package com.lizhouwei.chapter8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 在数组中找到出现次数大于N/K 的数
 * @Author: lizhouwei
 * @CreateDate: 2018/5/7 19:58
 * @Modify by:
 * @ModifyDate:
 */
public class Chapter8_6 {
    public void printHalfMajor(int[] arr) {
        int cand = 0;
        int times = 0;
        for (int i = 0; i < arr.length; i++) {
            if (times == 0) {
                cand = arr[i];
                times++;
            } else if (cand == arr[i]) {
                times++;
            } else {
                times--;
            }
        }
        times = 0;
        for (int i = 0; i < arr.length; i++) {
            if (cand == arr[i]) {
                times++;
            }
        }
        if (times > arr.length / 2) {
            System.out.println("超过一半的元素为:" + cand);
        }
    }

    public void printKMajor(int[] arr, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        int item = 0;
        for (int i = 0; i < arr.length; i++) {
            item = arr[i];
            if (map.containsKey(item)) {
                map.put(item, map.get(item) + 1);
            } else if (map.size() ==k - 1) {
                allCandMinusOne(map);
            } else {
                map.put(item, 1);
            }
        }
        Map<Integer, Integer> resultMap = getReals(arr, map);
        System.out.print("个数超过" + arr.length / k + "的元素为:");
        for (Integer key : resultMap.keySet()) {
            if (resultMap.get(key) > arr.length / k) {
                System.out.print(key + " ");
            }
        }

    }

    public void allCandMinusOne(Map<Integer, Integer> map) {
        List<Integer> removeList = new ArrayList<>();
        for (Integer key : map.keySet()) {
            map.put(key, map.get(key) - 1);
            if (map.get(key) == 0) {
                removeList.add(key);
            }
        }
        for (Integer key : removeList) {
            map.remove(key);
        }
    }

    public Map<Integer, Integer> getReals(int[] arr, Map<Integer, Integer> map) {
        Map<Integer, Integer> reals = new HashMap<>();
        int item = 0;
        for (int i = 0; i < arr.length; i++) {
            item = arr[i];
            if (map.containsKey(item)) {
                if (reals.containsKey(item)) {
                    reals.put(item, reals.get(item) + 1);
                } else {
                    reals.put(item, 1);
                }
            }
        }
        return reals;
    }

    //测试
    public static void main(String[] args) {
        Chapter8_6 chapter = new Chapter8_6();
        int[] arr = {1, 2, 2, 2, 2, 2, 3, 3};
        System.out.print("数组{1, 2,2, 2, 2, 3, 3}中");
        chapter.printHalfMajor(arr);
        int[] arr1 = {1, 2, 2, 2, 2, 3, 3, 3, 3};
        System.out.print("数组{1, 2, 2, 2, 2, 3, 3, 3, 3}中");
        chapter.printKMajor(arr1,4);

    }
}

结果

posted @ 2018-05-07 20:49  lizhouwei  阅读(119)  评论(0编辑  收藏  举报