//先定义一个数据结构,代表一条微博,有两个内容:发布者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设计实现一个微博系统的更多相关文章

  1. [LeetCode] 355. Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  2. 355 Design Twitter 设计推特

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能:    postTweet(userI ...

  3. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  4. [LeetCode] Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  5. 【LeetCode】355. Design Twitter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  7. leetcode 355 Design Twitte

    题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...

  8. [LeetCode] 534. Design TinyURL 设计短网址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  9. 355. Design Twitter

    二刷尝试了别的办法,用MAP代表关注列表. 然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...

随机推荐

  1. C语言中 EXIT_FAILURE和EXIT_SUCCESS

    1.C语言中 宏EXIT_FAILURE和EXIT_SUCCESS定义在头文件stdlib.h中,是一个符号常量,定义如下: #define EXIT_FAILURE 1 #define EXIT_S ...

  2. 笔记本无法连接校园网,windows诊断显示校园网之未响应

    打开cmd(管理员): 输入以下四条,每一条都按enter ipconfig /flushdns ipconfig /registerdns ipconfig /release ipconfig / ...

  3. Mac MySQL 8.0 (免安装版) 主从集群搭建

    一.下载解压包 打开 MySQL 官网地址:https://dev.mysql.com/downloads/mysql/ ,选择面安装版本. 二.解压文件 下载到合适文件夹,解压压缩包. 解压 mys ...

  4. Fiddler 4 断点调试(修改request请求参数)

    1.选中要测试的链接 2然后点击规则的Automatic Breakpoints 的Before Requests 3.重新发送请求找到测试的点链接 最终效果如下

  5. 条件循环语句组成了Python代码的骨架

    条件控制 我们都知道流程图是有多个分支的,程序中也是如此,在Python中是用if语句来判断程序该走哪个分支的.它的执行过程如下: 代码执行过程如下: if if语句的一般形式如下: if condi ...

  6. Panda Global获悉,美国承诺4年内明确区块链数字资产监管方式!

    近日,美国商品期货交易委员会(CFTC)宣布,在4年内将会全面把加密货币监管列为优先事项.Panda Global从7月8日公布的新战略中获悉,此次CFTC公布了自己接下来的新框架,并且在框架中承诺: ...

  7. Codeforces Round #682 Div2 简要题解

    Contest link A.Specific Tastes of Andre Problem link 题意 构造一个长度为 \(n\) 的序列,使得每个非空子序列的和都被其长度整除. 思路 直接每 ...

  8. asp.net在线人数限制

    1.网站启动初始化在线人数变量 Application["WebsiteCount"] = 0; 2.新的会话进来 只有在全新的会话进来的时候,该方法才会执行.可以过滤掉某些不需要 ...

  9. KafkaMirrorMaker 的不足以及一些改进

    背景 某系统使用 Kafka 存储实时的行情数据,为了保证数据的实时性,需要在多地机房维护多个 Kafka 集群,并将行情数据同步到这些集群上. 一个常用的方案就是官方提供的 KafkaMirrorM ...

  10. Java NIO网络编程demo

    使用Java NIO进行网络编程,看下服务端的例子 import java.io.IOException; import java.net.InetAddress; import java.net.I ...