当前位置: 首页 > 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.mrgr.cn/news/4987.html

相关文章:

  • 连锁门店收银系统源码+电子发票
  • PHP进阶篇(奇怪的知识又增加了)
  • 【回文数判断】输入一个5位数,判断它是不是回文数
  • 【Leetcode 1436 】旅行终点站—— 哈希表
  • xss靶场详解
  • Java笔试面试题AI答之线程(2)
  • 笔记分享: 香港中文大学CSCI5610高级数据结构——最邻近查询
  • 软件定义网络(SDN):网络架构的未来
  • 【pyside6】添加应用图标
  • Java-什么是缓存线程池?
  • 政务大数据解决方案(六)
  • 提升职业竞争力,亚马逊云科技认证助你云端腾飞
  • QT-贪吃蛇小游戏
  • 信号特征之希尔伯特变换(Python、C++、MATLAB实现)
  • .NET开源纪元:穿越封闭的迷雾,拥抱开放的星辰
  • 49.给出一个字符串数组,实现一个算法给定一组字符串,将字母异位词组合在一起
  • 【图文并茂】ant design pro 如何统一封装好 ProFormSelect 的查询请求
  • 如何在Linux系统中放大MKV视频文件的音量
  • 复制带随机指针的链表
  • Hadoop入门基础(一):深入探索Hadoop内部处理流程与核心三剑客