[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里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...
随机推荐
- 如何在word中插入代码
本文使用的是word2007,在网上查阅资料,可以使用如下方法: 1. 插入一个1行1列的表格,然后将代码写在里面,完成之后选中表格: 2. 将样式改为"HTML代码". 其实只是 ...
- 【NOIP2015模拟11.5】JZOJ8月5日提高组T3 旅行
[NOIP2015模拟11.5]JZOJ8月5日提高组T3 旅行 题目 若不存在第\(k\)短路径时,输出"Stupid Mike" 题解 题意 给出一个有\(n\)个点的树 问这 ...
- PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件当前项类属性currentIndex、currentTabText、currentTabName、currentTabIcon介绍
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTabWidget的当前项类属性为用于访问当前操作的选项卡,包括如下属性: 1. currentI ...
- PyQt(Python+Qt)学习随笔:QListWidgetItem的构造方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidgetItem对象专门用于作为QListWidget对象的一个项. QListWid ...
- 剑指offer二刷——数组专题——构建乘积数组
构建乘积数组 题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A ...
- 串口数据监视-Bus Hound
Bus Hound使用说明 一.打开该工具,会看到最上面的六个图标:1.Capture(捕捉按钮):按下它选择捕捉数据界面2.Save(保存按钮):按下它选择保存数据界面3.Setting(设置按钮) ...
- nginx学习之——CentOS6.0下安装nginx
1.下载对应nginx版本 #注:下载地址:http://nginx.org/download/ wget -c http://nginx.org/download/nginx-1.10.3.tar. ...
- centos7安装zabbix server5.0
安装zabbix源 1.rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarc ...
- java-01-基础语法
1. 注释 单行注释 格式: // 注释信息 多行注释 格式: /* 注释信息 */ 文档注释 格式: /** 注释信息 */ 2. 常量 定义:在程序运行过程中,其值不可发生改变的量 分类: 常量类 ...
- 想用selenium ,先了解html 基础知识(5)
二.HTML语法---了解!1.HTML超文本标记语言,是网页设计使用的语言.2.从<html>开始,到</html>结束,里面包括head和body两个部分,我们测试人员关心 ...