File tree Expand file tree Collapse file tree 3 files changed +48
-24
lines changed Expand file tree Collapse file tree 3 files changed +48
-24
lines changed Original file line number Diff line number Diff line change 388
388
| 684| [ Redundant Connection] ( https://leetcode.com/problems/redundant-connection/ ) | [ Java] ( leetcode/solution/src/RedundantConnection.java ) |||
389
389
| 695| [ Max Area of Island] ( https://leetcode.com/problems/max-area-of-island/ ) | [ Java] ( leetcode/solution/src/MaxAreaOfIsland.java ) | 100||
390
390
| 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||
391
392
| 904| [ Fruit Into Baskets] ( https://leetcode.com/problems/fruit-into-baskets/ ) | [ Java] ( leetcode/solution/src/FruitIntoBaskets.java ) | 90||
392
393
| 929| [ Unique Email Addresses] ( https://leetcode.com/problems/unique-email-addresses/ ) | [ Java] ( leetcode/solution/src/UniqueEmailAddresses.java ) | 90||
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -4,34 +4,13 @@ public class Main {
4
4
5
5
public static class Solution {
6
6
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
+
31
8
}
32
9
33
10
34
11
public static void main (String [] args ) {
35
12
Solution solution = new Solution ();
13
+ String s = solution .mostCommonWord ("Bob" , new String [0 ]);
14
+ System .out .println (s );
36
15
}
37
16
}
You can’t perform that action at this time.
0 commit comments