Skip to content

Commit 219b4df

Browse files
committed
fd
1 parent 418bfd2 commit 219b4df

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,6 @@
388388
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/)| [Java](leetcode/solution/src/RedundantConnection.java)|||
389389
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/)|[Java](leetcode/solution/src/MaxAreaOfIsland.java)|100||
390390
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Java](leetcode/solution/src/JewelsAndStones.java)|100||
391+
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Java](leetcode/solution/src/MostCommonWord.java)|85||
391392
|904|[Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/)|[Java](leetcode/solution/src/FruitIntoBaskets.java)|90||
392393
|929|[Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/)|[Java](leetcode/solution/src/UniqueEmailAddresses.java)|90||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
5+
public class MostCommonWord {
6+
7+
/**
8+
* 要注意结尾的单词
9+
*/
10+
public String mostCommonWord(String paragraph, String[] banned) {
11+
HashMap<String, Integer> map = new HashMap<>();
12+
HashSet<String> bannes = new HashSet<>(Arrays.asList(banned));
13+
14+
String result = null;
15+
int count = 0;
16+
17+
boolean enterWord = false;
18+
for (int i = 0, j = 0; j <= paragraph.length(); j++) {
19+
char c = j < paragraph.length() ? paragraph.charAt(j) : '.';
20+
if (Character.isAlphabetic(c)) {
21+
if (!enterWord) {
22+
enterWord = true;
23+
i = j;
24+
}
25+
} else {
26+
if (enterWord) {
27+
enterWord = false;
28+
String word = paragraph.substring(i, j).toLowerCase();
29+
if (!bannes.contains(word)) {
30+
int cnt = map.getOrDefault(word, 0) + 1;
31+
map.put(word, cnt);
32+
33+
if (cnt > count) {
34+
count = cnt;
35+
result = word;
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
return result;
43+
}
44+
}

leetcode/src/Main.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,13 @@ public class Main {
44

55
public static class Solution {
66

7-
public List<Integer> topKFrequent(int[] nums, int k) {
8-
HashMap<Integer, Integer> map = new HashMap<>();
9-
int max = 0;
10-
for (int n : nums) {
11-
int cnt = map.getOrDefault(n, 0) + 1;
12-
map.put(n, cnt);
13-
max = Math.max(max, cnt);
14-
}
15-
List<Integer>[] lists = new ArrayList[max + 1];
16-
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
17-
int freq = entry.getValue();
18-
if (lists[freq] == null) {
19-
lists[freq] = new ArrayList<>();
20-
}
21-
lists[freq].add(entry.getKey());
22-
}
23-
List<Integer> result = new ArrayList<>();
24-
for (int i = max; i >= 0 && result.size() <= k; i--) {
25-
if (lists[i] != null) {
26-
result.addAll(lists[i]);
27-
}
28-
}
29-
return result.subList(0, k);
30-
}
7+
318
}
329

3310

3411
public static void main(String[] args) {
3512
Solution solution = new Solution();
13+
String s = solution.mostCommonWord("Bob", new String[0]);
14+
System.out.println(s);
3615
}
3716
}

0 commit comments

Comments
 (0)