设计Twitter的api
355. Design Twitter
题意:设计Twitter的API,实现以下功能。
- 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.
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的更多相关文章
- Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj
Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj 1. 俩个问题::识别垃圾文件与清理策略1 2. 如何识别垃圾图片1 2.1. 体积过小文件<1 ...
- 如何设计优秀的API(转)
到目前为止,已经负责API接近两年了,这两年中发现现有的API存在的问题越来越多,但很多API一旦发布后就不再能修改了,即时升级和维护是必须的.一旦API发生变化,就可能对相关的调用者带来巨大的代价, ...
- 队列链式存储 - 设计与实现 - API函数
队列相关基础内容参我的博文:队列顺序存储 - 设计与实现 - API函数 队列也是一种特殊的线性表:可以用线性表链式存储来模拟队列的链式存储. 主要代码: // linkqueue.h // 队列链式 ...
- 队列顺序存储 - 设计与实现 - API函数
队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操作! queue常用操作 销毁队列 清空队 ...
- OAuth 2和JWT - 如何设计安全的API?
OAuth 2和JWT - 如何设计安全的API? Moakap译,原文 OAuth 2 VS JSON Web Tokens: How to secure an API 本文会详细描述两种通用的保证 ...
- 【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 ...
- ATITIT.翻译模块的设计与实现 api attilax 总结
ATITIT.翻译模块的设计与实现 api attilax 总结 1. 翻译原理1 2. TMX格式是国际通用格式(xml)1 2.1. 方法/步骤2 3. TRADOS2 4. ATITIT.翻译软 ...
- 使用JWT设计SpringBoot项目api接口安全服务
转载直: 使用JWT设计SpringBoot项目api接口安全服务
- 如何构建和设计以确保 API 的安全性
如何构建和设计以确保 API 的安全性 面对常见的OWASP十大威胁.未经授权的访问.拒绝服务攻击.以及窃取机密数据等类型的攻击,企业需要使用通用的安全框架,来保护其REST API,并保证良好的用户 ...
随机推荐
- springboot 配置文件的加载顺序
springboot 配置文件的加载顺序1.在命令行中传入的参数.2. SPRING APPLICATION JSON中的属性. SPRING_APPLICATION—JSON是以JSON格式配置在系 ...
- react踩坑
1.列表渲染一定要注意key的唯一性,不然会导致页面只更新部分的bug graphVertexConfigList.data.map((item, index) => { return < ...
- 记录一下list转map
Map<String,String> mapListStream = list.stream().collect(Collectors.toMap(key,value)):key对应key ...
- spark算子之Aggregate
Aggregate函数 一.源码定义 /** * Aggregate the elements of each partition, and then the results for all the ...
- Python 调用自己编写的Class
假设自己写的 class 文件myPets.py放在当前目录的子目录/myClasses下,在myPets.py中定义了一个 class 叫Pet.现在要调用Pet这个 class : from my ...
- 阿里云智能推荐AIRec产品介绍
一.智能推荐(AIRec)简介 AIRec背景介绍 智能推荐(简称AIRec)基于阿里巴巴集团领先的大数据和人工智能技术,结合阿里巴巴在电商.内容.新闻.视频直播和社交等多个行业领域的积累,为全球企业 ...
- Gym 100431E Word Cover 题解:KMP上跑dp
题意: 给你一个串,问你他的每个前缀的最小重复单元,其中单元是可以重叠的,最后按顺序输出即可.比如样例中abaabaa的最小重复单元为abaa,所以相应输出为4. 样例: input : abaaba ...
- CF232E Quick Tortoise , Fzoj 3118
这一题由于数据较多,我们考虑离线处理. 分治.对于两个点s,t,如果起点在mid这条横线上方,终点在下方,那么它必定会穿过mid这条线.所以只要s可以到mid上一点x,x可以到t,st就是安全的. 用 ...
- delphi下运行vbscript脚本
简单一个vb脚本,功能为打开被限制的注册表.Set wso = CreateObject("WScript.Shell")wso.RegWrite "HKEY_CURRE ...
- LUOGU P5061 秘密任务(背包+二分图染色)
传送门 解题思路 \(orz\)出题人的神仙做法.本蒟蒻看不懂,就水个求补图再二分图染色的方法来\(%1%\)出题人. 首先我们对图中\(m\)个关系连边,发现这样是没法做的,因为我们最后要关注的是谁 ...