【Leetcode】355. Design Twitter
题目描述:
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:
- postTweet(userId, tweetId): Compose a new tweet.
- getNewsFeed(userId): 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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
题目分析:
这是一道实现具体类的题目,一般来说这样的题只要理解每个方法的具体用法就不难做出。在这道题目中,需要一个发twitter的方法,需要一个加关注和一个取消关注的方法,以及一个浏览twitter的方法。所以我们需要在类中有一个保存userId和他发送的所有twitter的关系的Map,以及userId和他所关注的其他userid的关系的Map。浏览twitter时只要找到用户自己发的twitter,和通过自己所关注的人所发的twitter,并从中选取出10条最近发送的即可。
这道题我在实现的时候花了比较长的时间,主要有两点没有注意到:
1.list的addAll()方法不能参数不能为null,否则会报错。
2.twitterId并不代表发送顺序,需要自己设置一个表示发送顺序的时间戳。
注意到以上两点,这个题就不难解决。
具体代码:
public class Twitter {
private static int order=0;
private Map<Integer,Set<Message>> messages;
private Map<Integer,Set<Integer>> followers;
/** Initialize your data structure here. */
public Twitter() {
messages = new HashMap<Integer,Set<Message>>();
followers = new HashMap<Integer,Set<Integer>>();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
Message m = new Message(userId,tweetId,order++);
Set<Message> set=messages.getOrDefault(userId, new HashSet<Message>());
set.add(m);
messages.put(userId, set);
}
/** 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. */
public List<Integer> getNewsFeed(int userId) {
List<Message> sets = new ArrayList<Message>();
//userId发布的消息
Set<Message> set = messages.getOrDefault(userId, new HashSet<Message>());
sets.addAll(set);
//找出他所关注的人发布的消息
Set<Integer> follow = followers.get(userId);
if(follow!=null){
for(int i:follow){
set=messages.getOrDefault(i, new HashSet<Message>());
sets.addAll(set);
}
}
//对找出的消息进行排序,并找出最近翻出的10条返回
List<Integer> result=new ArrayList<Integer>();
Compare c =new Compare();
sets.sort(c);
for(int i=0;i<sets.size()&&i<10;i++){
Message m=sets.get(i);
result.add(m.twitterId);
}
return result;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if(followeeId==followerId)
return;
Set<Integer> set = followers.getOrDefault(followerId, new HashSet<Integer>());
set.add(followeeId);
followers.put(followerId, set);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
if(followeeId==followerId)
return;
if(!followers.containsKey(followerId)){
;
}
else{
Set<Integer> set = followers.get(followerId);
set.remove(followeeId);
//如果userid的用户已经没有关注的人,讲他的记录从map中删除掉
if(set.size()==0){
followers.remove(followerId);
}
else{
followers.put(followerId, set);
}
}
}
}
//封装一条twitter的类
class Message{
int userId;
int twitterId;
int order;
public Message(int userId, int twitterId,int order) {
super();
this.userId = userId;
this.twitterId = twitterId;
this.order=order;
}
}
//对消息进行排序的比较器
class Compare implements Comparator<Message>{
@Override
public int compare(Message m1, Message m2) {
// TODO Auto-generated method stub
if(m1.order>m2.order)
return -1;
else if(m1.order<m2.order)
return 1;
else
return 0;
}
}
【Leetcode】355. Design Twitter的更多相关文章
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】705. Design HashSet 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...
- 【leetcode】1244. Design A Leaderboard
题目如下: Design a Leaderboard class, which has 3 functions: addScore(playerId, score): Update the leade ...
- 【leetcode】622. Design Circular Queue
题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...
随机推荐
- 【WinForm】C# 发送Email
发送Email 的条件 1.SmtpClient SMTP 协议 即 Host 处理事务的主机或IP地址 //smtp.163.com UseDefaultCredentia ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- 使用CKEditor编辑器进行文本编辑
首先下载CKEditor. 下载地址:点击打开链接 将下载完的CKEditor解压而且导入到项目中. 然后在页面引入CKEditor <script type="text/javasc ...
- delphi 13 打印相关
打印 页面设置 打印预览 文档属性 //---------------------------------------------------------------------------- ...
- PhotoShop中画圆角矩形最简单方法(图文并茂)!
PhotoShop中画圆角矩形最简单方法(图文并茂)! 1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHhubjUyMA==/font/5a6L5L ...
- 分享一个圆角自定义的漂亮AlertDialog
\res\drawable-hdpi\bg_title_custom_dialog.xml: <?xml version="1.0" encoding="utf-8 ...
- SSH框架之Struts(2)——Struts的执行流程之配置文件
上篇我们大致了解了一下採用了Struts框架的web页面运行流程. 接下来的几篇我们通过Struts的源代码来学习一下Struts的内部原理. 当server启动的时候.server会依据配置文件初始 ...
- MySQL · BUG分析 · Rename table 死锁分析
http://mysql.taobao.org/monthly/2016/03/06/ 背景 InnoDB buffer pool中的page管理牵涉到两个链表,一个是lru链表,一个是flush 脏 ...
- du 和 df命令的区别(超赞)
du和df命令都被用于获得文件系统大小的信息:df用于报告文件系统的总块数及剩余块数,du -s /<filesystem>用于报告文件系统使用的块数.但是,我们可以发现从df命令算出的文 ...
- MAC SVN Phonegap
1. Windows上用VisualSVN Server Manager创建好Repository. 2. 在MAC上,用Phonegap创建好项目,比如在Project1目录里的App目录. 3. ...