当前位置: 首页 > news >正文

Golang | Leetcode Golang题解之第355题设计推特

题目:

题解:

type Twitter struct {Tweets []intUserTweets map[int][]intFollows map[int][]intIsFollowMy map[int]bool
}/** Initialize your data structure here. */
func Constructor() Twitter {// 每一次实例化的时候,都重新分配一次,这样不会造成示例重复var Tweets  []int// 某用户发的某条推特var UserTweets = make(map[int][]int)// 某用户关注了哪些用户var Follows = make(map[int][]int)var IsFollowMy = make(map[int]bool)t := Twitter{Tweets:Tweets,UserTweets:UserTweets,Follows: Follows,IsFollowMy: IsFollowMy,}return t
}/** Compose a new tweet. */
func (this *Twitter) PostTweet(userId int, tweetId int)  {// 每个人每次发推特,都记录到一个地方this.Tweets = append(this.Tweets,tweetId)// 某个用户发了推特,存到自己推特列表里this.UserTweets[userId] = append(this.UserTweets[userId],tweetId)
}/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
func (this *Twitter) GetNewsFeed(userId int) []int {fs := this.Follows[userId] // 先获取该用户的关注列表var allTweets []intfor _,v := range fs {// 把关注列表的人的所有推特都集中起来allTweets = append(allTweets,this.UserTweets[v]...)}if !this.IsFollowMy[userId] {// 如果自己没有关注自己,那么也需要把自己发的推特加到一起allTweets = append(allTweets,this.UserTweets[userId]...)}var sortTweets []intaTLen := len(this.Tweets)s := 0// 按照发的推特顺序进行倒序排序for i:=aTLen-1;i>=0;i-- {if s >= 10 {break}for _,n := range allTweets {// 只取 10条数据if this.Tweets[i] == n && s < 10{s++sortTweets = append(sortTweets,n)}}}return sortTweets
}/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
func (this *Twitter) Follow(followerId int, followeeId int)  {// 如果自己关注了自己,标记一下if followerId == followeeId {this.IsFollowMy[followerId] = true}// 下面是判断这人是否关注了,如果已经关注了,那么就不再关注了var isFed boolfor _,v := range this.Follows[followerId] {if v == followeeId {isFed = true}}if !isFed {this.Follows[followerId] = append(this.Follows[followerId],followeeId)}
}/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
func (this *Twitter) Unfollow(followerId int, followeeId int)  {// 如果自己取关了自己,标记一下if followeeId == followerId {this.IsFollowMy[followerId] = false}// 去掉自己关注列表里那个被关注的人var temp []intfor _,v := range this.Follows[followerId] {if v != followeeId {temp = append(temp,v)}}this.Follows[followerId] = temp
}
/*** Your Twitter object will be instantiated and called as such:* obj := Constructor();* obj.PostTweet(userId,tweetId);* param_2 := obj.GetNewsFeed(userId);* obj.Follow(followerId,followeeId);* obj.Unfollow(followerId,followeeId);*/
http://www.lryc.cn/news/430345.html

相关文章:

  • Redis如何实现发布/订阅?
  • EmguCV学习笔记 VB.Net 4.4 图像形态学
  • HarmonyOS 开发
  • 拒绝拖延!Kimi助你一天内速成论文初稿!
  • Python画笔案例-005 绘制迷宫
  • 【鸿蒙学习】HarmonyOS应用开发者高级认证 - 应用性能优化二(代码层面)
  • 【Docker】如何将A机器内的镜像,导入到B机器?
  • 动手实现基于Reactor模型的高并发Web服务器(一):epoll+多线程版本
  • 爬虫案例4——爬取房天下数据
  • 网络硬盘录像机NVR程序源码NVR全套运用方案
  • 03:电容的充放电特性及应用举例
  • 【专题】2023-2024中国游戏企业研发竞争力报告合集PDF分享(附原数据表)
  • 会话跟踪方案:Cookie Session Token
  • jemeter压力测试入门
  • SpringBoot3 简单集成 Spring AI 并使用
  • 【C/C++】程序设计基础知识(数据类型与表达式、控制语句、数组与结构)
  • python库——sklearn的关键组件和参数设置
  • CAS-ViT实战:使用CAS-ViT实现图像分类任务(一)
  • 处理数组下标的代码
  • 数学建模算法总结
  • 代码随想录算法训练营第五十五天 | 并查集理论基础、107. 寻找存在的路径
  • ROS_package 、CMakeLists.txt、package.xml、ROS_node之间的关系
  • 嵌入式学习----网络通信之TCP协议通信
  • 【信息学奥赛一本通】1007:计算(a+b)×c的值
  • Linux系统之部署俄罗斯方块网页小游戏(三)
  • XSS- - - DOM 破坏案例与靶场
  • Arco Design,字节跳动出品的UI库
  • 常用API:object
  • 【计算机三级-数据库技术】数据库后台编程技术
  • 线程的控制