migratoryBirds * output int arr element that
/** * migratoryBirds * output int arr element that occus frequence is most high and rank at the lowest down layer * constriants: * arrList numberSize: 5<=n<=2*10to5 * it is guaranteed that each arr element is 1,2,3,4,5 * @param arr * @return */ public static int migratoryBirds(List<Integer> arr){ if (arr==null){ return 0; } if (arr.isEmpty()){ return 0; } int targetSize=1; for (int i = 0; i < 5; i++) { targetSize*=10; } if (arr.size()<5 || arr.size()>targetSize){ return 0; } ArrayList<Integer> integers = new ArrayList<>(); integers.add(1); integers.add(2); integers.add(3); integers.add(4); integers.add(5); for (Integer integer : arr) { if (!(integers.contains(integer))){ return 0; } } HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>(); for (Integer integer : arr) { if (integerIntegerHashMap.containsKey(integer)){ integerIntegerHashMap.put(integer,integerIntegerHashMap.get(integer)+1); }else { integerIntegerHashMap.put(integer,1); } } ArrayList<CustomerIntStatistic> customerIntStatistics = new ArrayList<>(); integerIntegerHashMap.forEach((k,v)->{ CustomerIntStatistic customerIntStatistic = new CustomerIntStatistic(); customerIntStatistic.setId(UUID.randomUUID().toString()); customerIntStatistic.setArrIntEle(k); customerIntStatistic.setArrIntEleOccusFrequence(v); customerIntStatistics.add(customerIntStatistic); }); Collections.sort(customerIntStatistics, new Comparator<CustomerIntStatistic>() { @Override public int compare(CustomerIntStatistic o1, CustomerIntStatistic o2) { if (o1.getArrIntEleOccusFrequence()>o2.getArrIntEleOccusFrequence()){ return 1; } else if (o1.getArrIntEleOccusFrequence()<o2.getArrIntEleOccusFrequence()) { return -1; } else if (o1.getArrIntEleOccusFrequence()==o2.getArrIntEleOccusFrequence()) { if (o1.getArrIntEle()<o2.getArrIntEle()){ return 1; } else if (o1.getArrIntEle()>o2.getArrIntEle()) { return -1; } } return 0; } }); return customerIntStatistics.get(customerIntStatistics.size()-1).getArrIntEle(); } class CustomerIntStatistic{ private String id; private Integer arrIntEle; private Integer arrIntEleOccusFrequence; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getArrIntEle() { return arrIntEle; } public void setArrIntEle(Integer arrIntEle) { this.arrIntEle = arrIntEle; } public Integer getArrIntEleOccusFrequence() { return arrIntEleOccusFrequence; } public void setArrIntEleOccusFrequence(Integer arrIntEleOccusFrequence) { this.arrIntEleOccusFrequence = arrIntEleOccusFrequence; } @Override public String toString() { return "CustomerIntStatistic{" + "id='" + id + '\'' + ", arrIntEle=" + arrIntEle + ", arrIntEleOccusFrequence=" + arrIntEleOccusFrequence + '}'; } }#牛客创作赏金赛##如何缓解求职过程中的焦虑?#
Java技术 文章被收录于专栏
JavaEE技术 编程开发经验 企业通用技术