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. 微信小程序の微信js

    一.Javascript简介 二.nodejs中的jscript nodejs表示谷歌基于v8引擎的一门后端语言, ECMA表示ECMA262标准的基本js,native表示nodejs本身的一些包, ...

  2. tinkphp5.0目录结构说明

    tinkphp5.0目录结构说明 project 应用部署目录 ├─application 应用目录(可设置) │ ├─common 公共模块目录(可更改) │ ├─index 模块目录(可更改) │ ...

  3. Cannot proxy target class because CGLIB2 is not available .Add CGLIB to the class path or specify proxy interfaces…..

    报错:Cannot proxy target class because CGLIB2 is not available .Add CGLIB to the class path or specify ...

  4. ubuntu 下 使用 Git 维护 linux kernel版本

    学习linux内核一段时间,意识到内核的版本需要严格控制.利用Git工具可以很轻松的完成不同开发人员不同模块之间的代码融合与版本控制 . 1. 首先,安装Git .可以参考廖雪峰的博客  https: ...

  5. QPrinter

    在使用到QPrinter和QprintDialog类时的附加处理 ①若是在qt creator中,需要在 (.pro)工程文件中加入 “QT+= printsupport”,否则会编译报错

  6. 控制台Cannot read property 'disabled' of null报错的问题

    点击任何东西控制台都会报错: 也没有提示哪儿出了问题,后来我就代码一块一块的注释,终于找到了原因. 我在项目中用了 el-dropdown ,但是没有用他的el-dropdown-menu 所以才会一 ...

  7. Tomcat免安装版踩坑

    下载解压 从官网下载Tomcat的压缩包解压到硬盘上(这里用的是toncat7),解压之后目录如下(Windows) bin 存放tomcat的一些命令脚本 conf 存放配置文件 lib 存放运行时 ...

  8. Delphi 类(TObject、TPersistent、TComponent、TControl、TWinControl、TCustomControl、TGraphicControl、TInterfacedObject)简单介绍

      TObject:    VCL中所有类的根类,即是说:VCL中所有的类/组件/控件都是从TObject中继承而来.TObject类中定义了基本的 构造方法和析构方法.   TPersistent: ...

  9. 2018.12.26 考试(哈希,二分,状压dp)

    T1 传送门 解题思路 发现有一个限制是每个字母都必须相等,那么就可以转化成首尾的差值相等,然后就可以求出\(k-1\)位的差值\(hash\)一下.\(k\)为字符集大小,时间复杂度为\(O(nk) ...

  10. vue 复习篇. 注册全局组件,和 组件库

    初篇 ============================================================== 1. 编写loading组件(components/Loading/ ...