实现postContent(userId: int, contentId: int) -> None接口,根据给定userId和contentId,创建一条微博内容,注意contentId全局唯一。
时间: 2023-06-26 08:03:34 浏览: 41
可以考虑使用一个字典来存储微博内容,以contentId作为key,微博内容作为value。同时,可以使用一个列表来记录userId所发布的微博的contentId。
具体的实现方式如下:
```python
class Weibo:
def __init__(self):
self.contents = {} # 存储微博内容的字典
self.user_contents = {} # 存储每个用户发布的微博的contentId的列表
def postContent(self, userId: int, contentId: int) -> None:
# 如果contentId已经存在,则不创建微博内容
if contentId in self.contents:
return
# 创建微博内容
content = input("请输入微博内容:") # 可以根据实际情况获取微博内容
self.contents[contentId] = content
# 记录userId所发布的微博的contentId
if userId not in self.user_contents:
self.user_contents[userId] = [contentId]
else:
self.user_contents[userId].append(contentId)
```
在该代码中,我们首先判断contentId是否已经存在于contents字典中,如果存在,则直接返回,不需要创建微博内容。否则,根据实际情况获取微博内容,将其添加到contents字典中,并记录userId所发布的微博的contentId。