9.6腾讯第三题为什么是0,求大佬解答

import java.util.*;
import java.util.Map.Entry;
public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int N = in.nextInt();
			int K = in.nextInt();
			HashMap<String, Integer> map = new HashMap<>();
			for(int i = 0; i < N; i++){
				String cur = in.next();
				int num = map.getOrDefault(cur, 0);
				num++;
				map.put(cur, num);
			}
			Set<Map.Entry<String,Integer>> mapEntries = map.entrySet();
			List<Map.Entry<String,Integer>> aList = new LinkedList<Map.Entry<String,Integer>>(mapEntries); 
			Collections.sort(aList, new Comparator<Entry<String,Integer>>() { 	 
				public int compare(Map.Entry<String,Integer>e1,Map.Entry<String,Integer>e2){
	                int re = e2.getValue().compareTo(e1.getValue());
	                if(re!=0){return re;}
	                else{return e1.getKey().compareTo(e2.getKey());}
	            }
	        }); 
			for(int i=0;i<K;i++){
				System.out.printf("%s %d\n", aList.get(i).getKey(), aList.get(i).getValue());
	        }
			List<Map.Entry<String,Integer>> bList = new LinkedList<Map.Entry<String,Integer>>(mapEntries); 
			Collections.sort(bList, new Comparator<Entry<String,Integer>>() { 	 
				public int compare(Map.Entry<String,Integer>e1,Map.Entry<String,Integer>e2){
	                int re = e1.getValue().compareTo(e2.getValue());
	                if(re!=0){return re;}
	                else{return e1.getKey().compareTo(e2.getKey());}
	            }
	        }); 
			for(int i=0;i<K;i++){
				System.out.printf("%s %d\n", bList.get(i).getKey(), bList.get(i).getValue());
	        }
		}
		
		
	}

}

#腾讯##笔试题目#
全部评论
你参考下这个:https://www.nowcoder.com/discuss/501159?source_id=profile_create&channel=666
点赞
送花
回复 分享
发布于 2020-09-06 23:07

相关推荐

点赞 评论 收藏
分享
点赞 1 评论
分享
牛客网
牛客企业服务