//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容)
class TwitterData
{
int userId;
int twitterId;
public TwitterData(int userId,int twitterId)
{
this.twitterId = twitterId;
this.userId = userId;
}
}
//用一个map存储用户和各用户的关注用户
Map<Integer,Set<Integer>> userManager ;
//用一个linkedList来存储所有微博
List<TwitterData> twitterDatas ;
/** Initialize your data structure here. */
public Twitter() {
userManager = new HashMap<>();
twitterDatas= new LinkedList<>();
} /** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
//用户名如果没有注册,那就自动注册
if (!userManager.containsKey(userId))
{
Set<Integer> follows = new HashSet<>();
//这里注意,要想看到自己的微博,也要关注自己
follows.add(userId);
userManager.put(userId,follows);
}
TwitterData t = new TwitterData(userId,tweetId);
//添加到微博列表,每次都是添加到头部,代表是新的
twitterDatas.add(0,t);
} /** 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. */
//这里的意思是显示userId关注的人的前10条微博,而不是userId的微博
public List<Integer> getNewsFeed(int userId) {
List<Integer> res = new ArrayList<>();
//用户如果不存在,那么就不显示
if (userManager.containsKey(userId))
{
Set<Integer> follows = userManager.get(userId);
//计数,只显示10条
int count = 0;
for (TwitterData t:
twitterDatas) {
if (count==10) break;
//只添加关注用户的微博
if (follows.contains(t.userId))
{
res.add(t.twitterId);
count++;
}
}
}
return res;
} /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
//er是主动方,ee是被动方
//考虑两个用户不存在的情况
Set<Integer> follows;
if (!userManager.containsKey(followerId))
{
follows = new HashSet<>();
follows.add(followerId);
userManager.put(followerId,follows);
}
if (!userManager.containsKey(followeeId))
{
follows = new HashSet<>();
follows.add(followeeId);
userManager.put(followeeId,follows);
}
//两者都存在后
follows = userManager.get(followerId);
follows.add(followeeId);
} /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
//排除各种情况后删除就行
if (followerId!=followeeId&&userManager.containsKey(followerId)&&userManager.containsKey(followeeId))
userManager.get(followerId).remove(followeeId);
}

最新文章

  1. css3 transition属性
  2. Introduction to graph theory 图论/脑网络基础
  3. 基于ZK构建统一配置中心的方案和实践
  4. Js(DOM) 和Jq 对象的相互转换
  5. Debian Environment Variables
  6. 移动Web开发规范
  7. 没有Iphone也能装逼:让Android版QQ显示成Iphone6
  8. 值得学习的C语言开源项目
  9. 【hdu4366】dfs序线段树
  10. [Android设计模式]Android退出应用程序终极方法
  11. sqlserver取得本月一号
  12. 关于onClick 提交数据问题
  13. WPF button 如何区分click和doubleclick
  14. Windows 服务 创建 和 安装 -摘自网络
  15. java多线程编程题之连续打印abc的几种解法
  16. [CVPR2017] Deep Self-Taught Learning for Weakly Supervised Object Localization 论文笔记
  17. 3 - Two Pointers Algorithm
  18. 纸壳CMS主题增强,支持主题中加入模板
  19. 修改MySQL的时区,涉及参数time_zone
  20. Unity IOC容器通过配置实现类型映射的几种基本使用方法

热门文章

  1. linq 查询的结果会开辟新的内存吗?
  2. Django----Modelviewset继承
  3. sql中count(1)、count(*)与count(列名)的区别
  4. 使用paho的MQTT时遇到的重连导致订阅无法收到问题和解决
  5. moviepy音视频剪辑:TextClip.list(font)和search搜索字体报错UnicodeDecodeError:utf-8 codec cannott decode byte 问题
  6. Python函数中的位置参数
  7. 转:Chrome浏览器查看网站登录 Cookie 信息的方法
  8. LSB隐写加密MISC
  9. bugku never give up
  10. Aap.Net中的Action和Func委托