public String rankTeams(String[] votes) {
//key是参赛团队,value是该团队每个排位获得的票数
Map<Character, int[]> teamRankMap = new HashMap<>();
for (String vote : votes) {
for (int i = 0; i < vote.length(); i++) {
int[] rankArr = teamRankMap.getOrDefault(vote.charAt(i), new int[26]);
rankArr[i]++;
teamRankMap.put(vote.charAt(i), rankArr);
}
}
List<Map.Entry<Character, int[]>> teamRankList = new ArrayList<>(teamRankMap.entrySet());
Collections.sort(teamRankList, (team1, team2) -> {
int[] ranks1 = team1.getValue();
int[] ranks2 = team2.getValue();
//根据投票排序
for (int i = 0; i < 26; i++) {
if (ranks1[i] != ranks2[i]) {
return ranks2[i] - ranks1[i];
}
}
//字母顺序排序
return team1.getKey() - team2.getKey();
});
//转换为字符串输出
return teamRankList.stream().map(entry -> String.valueOf(entry.getKey())).collect(Collectors.joining());
}
作者:soap88
链接:https://leetcode-cn.com/problems/rank-teams-by-votes/solution/javapai-xu-jiu-hao-by-soap88/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。