3527. 找到最常见的回答

题目链接:https://leetcode.cn/problems/find-the-most-common-response/description/

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public String findCommonResponse(List<List<String>> responses) {
int maxCount = 0;
String ans = "";
Map<String, Integer> map = new HashMap<>();
Set<String> mySet = new HashSet<>();
for(List<String> list : responses) {
mySet.clear();
for(String word : list) {
if(!mySet.add(word)) {
continue;
}

if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
int count = map.get(word);
if (count > maxCount || (count == maxCount && word.compareTo(ans) < 0)) {
maxCount = count;
ans = word;
}
}
}

return ans;
}
}

3527. 找到最常见的回答
https://pisces34.github.io/2025/05/08/leetcode/3527/
发布于
2025年5月8日
许可协议