C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class TweetCounts {
public:
TweetCounts() {
}
void recordTweet(string tweetName, int time) {
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
}
};
/**
* Your TweetCounts object will be instantiated and called as such:
* TweetCounts* obj = new TweetCounts();
* obj->recordTweet(tweetName,time);
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/
运行代码
提交
golang 解法, 执行用时: 196 ms, 内存消耗: 9.7 MB, 提交时间: 2023-09-11 11:39:05
import "sort"
type TweetCounts struct {
tweets map[string][]int
}
func Constructor() TweetCounts {
tc := TweetCounts{
tweets: map[string][]int{},
}
return tc
}
func (this *TweetCounts) RecordTweet(tweetName string, time int) {
this.tweets[tweetName] = append(this.tweets[tweetName], time)
}
func (this *TweetCounts) GetTweetCountsPerFrequency(freq string, tweetName string, startTime int, endTime int) (rst []int) {
sort.Ints(this.tweets[tweetName])
var freqSecond = 60
switch freq {
case "minute":
break
case "hour":
freqSecond = 3600
case "day":
freqSecond = 24 * 3600
}
var tweets = this.tweets[tweetName]
rst = make([]int, (endTime-startTime)/freqSecond+1)
for i := 0; i < len(tweets); i++ {
if tweets[i] < startTime {
continue
}
if tweets[i] > endTime {
return rst
}
rst[(tweets[i]-startTime)/freqSecond]++
}
return rst
}
/**
* Your TweetCounts object will be instantiated and called as such:
* obj := Constructor();
* obj.RecordTweet(tweetName,time);
* param_2 := obj.GetTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/
cpp 解法, 执行用时: 92 ms, 内存消耗: 37.1 MB, 提交时间: 2023-09-11 11:36:36
class TweetCounts {
map<string, vector<int>> user;
public:
TweetCounts() {
}
void recordTweet(string tweetName, int time) {
user[tweetName].push_back(time);
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) {
endTime += 1;
int length = 0;
if (freq == "minute")
length = 60;
else if (freq == "hour")
length = 60 * 60;
else
length = 60 * 60 * 24;
vector<int> ans((endTime - startTime - 1) / length + 1);
for (int time : user[tweetName])
if (time >= startTime && time < endTime)
++ans[(time - startTime) / length];
return ans;
}
};
/**
* Your TweetCounts object will be instantiated and called as such:
* TweetCounts* obj = new TweetCounts();
* obj->recordTweet(tweetName,time);
* vector<int> param_2 = obj->getTweetCountsPerFrequency(freq,tweetName,startTime,endTime);
*/
python3 解法, 执行用时: 416 ms, 内存消耗: 22.4 MB, 提交时间: 2023-09-11 11:36:17
'''
方法一:线性表暴力
使用字典保存用户,按照用户名使用线性表存储其相应的所有推文,
查询时遍历该用户的所有推文判断是否在要求的时间区间内。
'''
class TweetCounts:
def __init__(self):
self.user = collections.defaultdict(list)
def recordTweet(self, tweetName: str, time: int) -> None:
self.user[tweetName].append(time)
def getTweetCountsPerFrequency(self, freq: str, tweetName: str, startTime: int, endTime: int) -> List[int]:
endTime += 1
if freq == 'minute':
length = 60
elif freq == 'hour':
length = 60 * 60
else:
length = 60 * 60 * 24
ans = [0] * ((endTime - startTime - 1) // length + 1)
for t in self.user[tweetName]:
if endTime > t >= startTime:
ans[(t - startTime) // length] += 1
return ans
# Your TweetCounts object will be instantiated and called as such:
# obj = TweetCounts()
# obj.recordTweet(tweetName,time)
# param_2 = obj.getTweetCountsPerFrequency(freq,tweetName,startTime,endTime)