@@ -31,7 +31,11 @@ wordsFrequency.get("pen"); //返回1
31
31
32
32
<!-- 这里可写通用的实现逻辑 -->
33
33
34
- 计数器实现。
34
+ ** 方法一:哈希表**
35
+
36
+ 我们用哈希表 ` cnt ` 统计每个单词出现的次数,` get ` 函数直接返回 ` cnt[word] ` 即可。
37
+
38
+ 初始化哈希表 ` cnt ` 的时间复杂度为 $O(n)$,其中 $n$ 为 ` book ` 的长度。` get ` 函数的时间复杂度为 $O(1)$。空间复杂度为 $O(n)$。
35
39
36
40
<!-- tabs:start -->
37
41
@@ -42,10 +46,10 @@ wordsFrequency.get("pen"); //返回1
42
46
``` python
43
47
class WordsFrequency :
44
48
def __init__ (self , book : List[str ]):
45
- self .counter = Counter(book)
49
+ self .cnt = Counter(book)
46
50
47
51
def get (self , word : str ) -> int :
48
- return self .counter [word]
52
+ return self .cnt [word]
49
53
50
54
51
55
# Your WordsFrequency object will be instantiated and called as such:
@@ -59,17 +63,16 @@ class WordsFrequency:
59
63
60
64
``` java
61
65
class WordsFrequency {
62
-
63
- private Map<String , Integer > counter = new HashMap<> ();
66
+ private Map<String , Integer > cnt = new HashMap<> ();
64
67
65
68
public WordsFrequency (String [] book ) {
66
- for (String word : book) {
67
- counter . put(word, counter . getOrDefault(word, 0 ) + 1 );
69
+ for (String x : book) {
70
+ cnt . merge(x, 1 , Integer :: sum );
68
71
}
69
72
}
70
73
71
74
public int get (String word ) {
72
- return counter . containsKey (word) ? counter . get(word) : 0 ;
75
+ return cnt . getOrDefault (word, 0 ) ;
73
76
}
74
77
}
75
78
@@ -80,16 +83,68 @@ class WordsFrequency {
80
83
*/
81
84
```
82
85
86
+ ### ** C++**
87
+
88
+ ``` cpp
89
+ class WordsFrequency {
90
+ public:
91
+ WordsFrequency(vector<string >& book) {
92
+ for (auto& x : book) {
93
+ ++cnt[ x] ;
94
+ }
95
+ }
96
+
97
+ int get(string word) {
98
+ return cnt[word];
99
+ }
100
+
101
+ private:
102
+ unordered_map<string, int > cnt;
103
+ };
104
+
105
+ /* *
106
+ * Your WordsFrequency object will be instantiated and called as such:
107
+ * WordsFrequency* obj = new WordsFrequency(book);
108
+ * int param_1 = obj->get(word);
109
+ */
110
+ ```
111
+
112
+ ### ** Go**
113
+
114
+ ``` go
115
+ type WordsFrequency struct {
116
+ cnt map [string ]int
117
+ }
118
+
119
+ func Constructor (book []string ) WordsFrequency {
120
+ cnt := map [string ]int {}
121
+ for _ , x := range book {
122
+ cnt[x]++
123
+ }
124
+ return WordsFrequency{cnt}
125
+ }
126
+
127
+ func (this *WordsFrequency ) Get (word string ) int {
128
+ return this.cnt [word]
129
+ }
130
+
131
+ /* *
132
+ * Your WordsFrequency object will be instantiated and called as such:
133
+ * obj := Constructor(book);
134
+ * param_1 := obj.Get(word);
135
+ */
136
+ ```
137
+
83
138
### ** JavaScript**
84
139
85
140
``` js
86
141
/**
87
142
* @param {string[]} book
88
143
*/
89
144
var WordsFrequency = function (book ) {
90
- this .counter = {} ;
91
- for (const word of book) {
92
- this .counter [word] = (this .counter [word] || 0 ) + 1 ;
145
+ this .cnt = new Map () ;
146
+ for (const x of book) {
147
+ this .cnt . set (x, (this .cnt . get (x) || 0 ) + 1 ) ;
93
148
}
94
149
};
95
150
@@ -98,7 +153,7 @@ var WordsFrequency = function (book) {
98
153
* @return {number}
99
154
*/
100
155
WordsFrequency .prototype .get = function (word ) {
101
- return this .counter [ word] || 0 ;
156
+ return this .cnt . get ( word) || 0 ;
102
157
};
103
158
104
159
/**
@@ -112,18 +167,18 @@ WordsFrequency.prototype.get = function (word) {
112
167
113
168
``` ts
114
169
class WordsFrequency {
115
- private map : Map <string , number >;
170
+ private cnt : Map <string , number >;
116
171
117
172
constructor (book : string []) {
118
- const map = new Map <string , number >();
173
+ const cnt = new Map <string , number >();
119
174
for (const word of book ) {
120
- map .set (word , (map .get (word ) ?? 0 ) + 1 );
175
+ cnt .set (word , (cnt .get (word ) ?? 0 ) + 1 );
121
176
}
122
- this .map = map ;
177
+ this .cnt = cnt ;
123
178
}
124
179
125
180
get(word : string ): number {
126
- return this .map .get (word ) ?? 0 ;
181
+ return this .cnt .get (word ) ?? 0 ;
127
182
}
128
183
}
129
184
0 commit comments