[leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者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);
}
[leetcode]355. Design Twitter设计实现一个微博系统的更多相关文章
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 355 Design Twitter 设计推特
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(userI ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- leetcode 355 Design Twitte
题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...
- [LeetCode] 534. Design TinyURL 设计短网址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- 355. Design Twitter
二刷尝试了别的办法,用MAP代表关注列表. 然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...
随机推荐
- centOs7.5.64以上版本的操作系统搭建GitLab记录
一. 安装并配置必要的依赖关系在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知) ,wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问. 1.安装ss ...
- 【刷题笔记】DP优化-单调队列优化
单调队列优化 眼界极窄的ZZ之前甚至不会单调队列--(好丢人啊) 单调队列优化的常见情景: 转移可以转化成只需要确定一个维度,而且这个维度的取值范围在某个区间里 修剪草坪 这个题学长讲的好像是另外一个 ...
- js如何动态创建一个新的标签
var DS; DS = CallIVRAjaxClass.GetBranchCallCount().value; var obj = {}; obj.branch = "_branch&q ...
- webpack系列:webpack小老弟接了个简单活
webpack深入浅出系列:进阶篇 前沿,本篇文章的讲解思路是以webpack的五大核心为线索,以webpack对象为第一视角来讲述(以前记得看过一个文笔非常厉害的技术啊婆写的,非常有趣.然后我就想着 ...
- 推荐4个Flutter重磅开源项目
早上好,骚年,我是小 G,我的公众号「菜鸟翻身」会推荐 GitHub 上有用的项目,一分钟 get 一个优秀的开源项目,挖掘开源的价值,欢迎关注我. 近年来,随着移动智能设备的快速普及,移动多端统一开 ...
- celery使用-win10和linux
win10启动方式 celery -A celery_tasks.main worker -l debug -P eventlet linux启动方式 /usr/local/bin/celery ce ...
- 第5.4节 Python函数中的变量及作用域
一.函数中的变量使用规则 函数执行时,使用的全局空间是调用方的全局空间,参数及函数使用的局部变量存储在函数单独的局部名字空间内: 函数的形参在函数中修改了值时,并不影响调用方本身的数据,但如果形参是一 ...
- RedHat-Linux操作指令第1篇
不同的linux系统切换方式会稍有一点差别 从图形界面切换到字符界面:Alt+F(1-8) 或者 Alt+Ctrl+Shift+F(1-8) 从字符界面切换回图形界面:Alt+F7 字符界面启动到图形 ...
- 题解-CF1239D Catowice City
CF1239D Catowice City 有 \(n\) 个人和 \(n\) 只猫.有 \(m\) 对人猫友谊,即第 \(u_i\) 个人认识第 \(v_i\) 只猫,保证第 \(i\) 个人和第 ...
- 笔记-[ZJOI2014]力
[ZJOI2014]力 \[\begin{split} E_j=&\sum_{i=1}^{j-1}\frac{q_i}{(i-j)^2}-\sum_{i=j+1}^{n}\frac{q_i}{ ...