题目描述:

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:

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. 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的更多相关文章

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

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

  2. 【LeetCode】1166. Design File System 解题报告 (C++)

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

  3. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  4. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  5. 【LeetCode】707. Design Linked List 解题报告(Python)

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

  6. 【LeetCode】706. Design HashMap 解题报告(Python)

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

  7. 【LeetCode】705. Design HashSet 解题报告(Python)

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

  8. 【leetcode】1244. Design A Leaderboard

    题目如下: Design a Leaderboard class, which has 3 functions: addScore(playerId, score): Update the leade ...

  9. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

随机推荐

  1. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

  2. 自定义的带tab的可左右滑动的viewpager之二viewpager与fragment不兼容

    总的来说,这个TAB用起来还算方便吧 不过随着用的地方多起来,发现了一些问题,比如下面这个界面: TAB1和TAB2都是表单,保存按钮对两个TAB都有效:若当前显示TAB1,点击保存则保存TAB1的f ...

  3. C++的XML编程经验――LIBXML2库使用指南[转]

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  4. 我总结的18个非常好用的vim指令

    在Linux下最有名的程序编辑器非vim莫属了. 在一般模式下, 1.dd——删除光标所在行 2./word ——全文搜索指定单词 3.G ——将光标移动到文件的最后一行,移动到第99行,就是99G ...

  5. [Angular-Scaled Web] 8. Using $http to load JSON data

    Using the $http service to make requests to remote servers. categories-model.js: angular.module('egg ...

  6. Android Configuration change属性

    问题:横竖屏切换时Activity的生命周期? 答案: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次 2 ...

  7. Unable to automatically debug "XXXXX“

    I solved this issue by going to C:\Program Files\Microsoft VisualStudio10.0\Common7\IDE then running ...

  8. 通用PE u盘启动盘制作

    导读 通用pe工具箱是现在最老牌的的U盘装系统和维护电脑的专用工具之一,一键式制作.操作简单便捷,几乎100%支持所有U盘,不再为装机烦恼们,抓紧时间下载通用pe工具箱体验下吧. 准备工作 ①从通用p ...

  9. SqlServer2008 之 应用积累

    1.断开数据库连接,在原有查询窗口(断开数据库连接的未关闭查询窗口),对现在所连数据库进行操作,结果是对已断开数据库的误操作. 正确操作:重新连接数据库后,应关闭原有查询窗口,新建查询窗口后再执行操作 ...

  10. C语言第四节数据类型、常量、变量

    数据 什么是数据 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据.图片数据.视频数据,还有聊QQ时产生的文字数据.用迅 ...