355. Design Twitter

题意:设计Twitter的API,实现以下功能。

  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.

Example:

Twitter twitter = new Twitter();

// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1); // User 1 follows user 2.
twitter.follow(1, 2); // User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6); // User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1); // User 1 unfollows user 2.
twitter.unfollow(1, 2); // User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);

以下是我的实现代码

大致思路:全局的信息队列,按发布时间排序。全局的用户映射表,存用户信息,设计成hash表结构,便于快速获取用户。

 public class Twitter {

     class User implements Comparable<User>{
int userId;
List<User> followers;
List<User> followees;
User(int userId) {
this.userId = userId;
this.followers = new LinkedList<>();
this.followees = new LinkedList<>();
}
public int compareTo(User o){
return userId-o.userId;
}
} class Message{
int userId;
int messageId;
Message(int userId, int messageId){
this.userId = userId;
this.messageId = messageId;
}
} private List<Message> messages;
private Map<Integer, User> users; /** Initialize your data structure here. */
public Twitter() {
messages = new LinkedList<>();
users = new HashMap<>();
} /** Compose a new tweet. */
public void postTweet(int userId, int tweetId) { if(!users.containsKey(userId)){
User u = new User(userId);
users.put(userId,u);
} Message m = new Message(userId, tweetId);
messages.add(0, m);
} /** 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<Integer> newMessages = new ArrayList<>();
Set<Integer> all = new HashSet<>();
all.add(userId);
if(users.containsKey(userId)) {
for (User u : users.get(userId).followees) {
all.add(u.userId);
}
}
int i=0;
for(Message m : messages){
if(all.contains(m.userId)){
newMessages.add(m.messageId);
i++;
if(i==10) break;
}
} return newMessages;
} /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
User u1;
User u2;
if(users.containsKey(followerId)){
u1 = users.get(followerId);
}else{
u1 = new User(followerId);
} if(users.containsKey(followeeId)){
u2 = users.get(followeeId);
}else{
u2 = new User(followeeId);
}
u1.followees.add(u2);
u2.followers.add(u1);
users.put(followerId, u1);
users.put(followeeId, u2);
} /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
User u1 = users.get(followerId);
User u2 = users.get(followeeId);
if(u1!=null)
u1.followees.remove(u2);
if(u2!=null)
u2.followers.remove(u1);
}
}

设计Twitter的api的更多相关文章

  1. Atitit.论图片类型 垃圾文件的识别与清理  流程与设计原则 与api概要设计 v2 pbj

    Atitit.论图片类型 垃圾文件的识别与清理  流程与设计原则 与api概要设计 v2 pbj 1. 俩个问题::识别垃圾文件与清理策略1 2. 如何识别垃圾图片1 2.1. 体积过小文件<1 ...

  2. 如何设计优秀的API(转)

    到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ...

  3. 队列链式存储 - 设计与实现 - API函数

    队列相关基础内容参我的博文:队列顺序存储 - 设计与实现 - API函数 队列也是一种特殊的线性表:可以用线性表链式存储来模拟队列的链式存储. 主要代码: // linkqueue.h // 队列链式 ...

  4. 队列顺序存储 - 设计与实现 - API函数

    队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...

  5. OAuth 2和JWT - 如何设计安全的API?

    OAuth 2和JWT - 如何设计安全的API? Moakap译,原文 OAuth 2 VS JSON Web Tokens: How to secure an API 本文会详细描述两种通用的保证 ...

  6. 【337】Text Mining Using Twitter Streaming API and Python

    Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...

  7. ATITIT.翻译模块的设计与实现 api attilax 总结

    ATITIT.翻译模块的设计与实现 api attilax 总结 1. 翻译原理1 2. TMX格式是国际通用格式(xml)1 2.1. 方法/步骤2 3. TRADOS2 4. ATITIT.翻译软 ...

  8. 使用JWT设计SpringBoot项目api接口安全服务

    转载直: 使用JWT设计SpringBoot项目api接口安全服务

  9. 如何构建和设计以确保 API 的安全性

    如何构建和设计以确保 API 的安全性 面对常见的OWASP十大威胁.未经授权的访问.拒绝服务攻击.以及窃取机密数据等类型的攻击,企业需要使用通用的安全框架,来保护其REST API,并保证良好的用户 ...

随机推荐

  1. css 实现水波纹,波浪动画效果

    <div class="wave"> 水波纹效果 <div class="wave1"></div> <div cla ...

  2. idea的spring配置

    开始使用idea,开始的时候把相关的插件都禁用了,导致在建项目和configuration的时候不出现spring相关字样 到plugins中找到installed的插件,查看spring boot ...

  3. 一文读懂MQTT协议

    1  概述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级 ...

  4. 一句话的Android增量更新框架(增量更新)

    转自:http://www.jianshu.com/p/a9ec8fa780e2 Android应用更新要使用完整的新版本Apk安装,增量更新则是提供一个新旧版本偏差数据的patch包供应用下载,然后 ...

  5. JS对象的引用,对象的拷贝

    目录 一.场景 二.浅拷贝 三.深拷贝 一.场景 除了基本类型跟null,对象之间的赋值,只是将地址指向同一个,而不是真正意义上的拷贝 将一个对象赋值给另外一个对象. var a = [1,2,3]; ...

  6. js滚动到顶部底部代码

    <!DOCTYPE HTML> <html> <head> <meta charset=UTF-8> <title>SCROLL</t ...

  7. Vim用法AAAAA

    .linux系统中如何进入退出vim编辑器,方法及区别 我们当然要保存并退出了,然后下一步了.这时,我们要按键盘左上角的"ESC",留意到了没有?左下角的插入状态不见了,如图. 然 ...

  8. 常用的HTTP请求头与响应头

    HTTP消息头是指,在超文本传输协议( Hypertext Transfer Protocol ,HTTP)的请求和响应消息中,协议头部分的那些组件.HTTP消息头用来准确描述正在获取的资源.服务器或 ...

  9. UVA - 10347 - Medians(由三中线求三角形面积)

    AC代码: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream> ...

  10. mysql优化3:BTree索引和Hash索引

    一.BTree索引 注:名叫btree索引,大的方面看,都用的平衡树,但具体的实现上,各引擎稍有不同,比如,严格地说,NDB引擎使用的是T-tree,Myisam和innodb中默认用B-tree索引 ...