设计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,并保证良好的用户 ...
随机推荐
- Python建立Tab自动补全的脚本
Python建立Tab自动补全的脚本 #!/usr/bin/python #python steup file import sys import readline import rlcomplete ...
- vue - blog开发学习1
1.安装vue-cli vue intall -g vue-cli 2.创建项目 vue init webpack nblog 3.按提示要求配置项目 ? Project name nblog ? P ...
- ubuntu 设置root密码
- css3两个汤圆亲吻动效
效果图: 模板来源:https://www.17sucai.com/pins/demo-show?id=35132 自己仿写出来的效果图: 笔记: 1.transform:translate(-50% ...
- 2018-2-13-win10-UWP-等级控件
title author date CreateTime categories win10 UWP 等级控件 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- swapon, swapoff - 使用/关闭用于分页和交换的文件和设备
总览 (SYNOPSIS) /sbin/swapon [-h -V] /sbin/swapon -a [-v] /sbin/swapon [-v] [-p priority] specialfile ...
- 六、原型(Prototype)模式
原型模式是对象的创建模式,通过给出一个原型对象来指明所要创建的对象的类型.然后用复制这个原型对象的方法来创建出更多同类型的对象. 原型模式可以不用重新初始化对象,而动态的获取对象运行时的状态.使用原型 ...
- 【JavaWeb项目】一个众筹网站的开发(八)后台页面详细设置
一.user.jsp改造 删除引入菜单 抽取导航栏 nav-bar.jsp,删除引入导航栏 删除引入main.jsp的到好烂 数据库里添加url 报错,url不对 没有/ url正确 action=& ...
- bootstrapTable 分页插件
前端: @{ ViewBag.Title = "BootstrapTable 入门"; Layout = null; } <!-- 引入bootstrap样式 --> ...
- 暴力枚举+扫描线+线段树——cf1194E
/*思路就是枚举矩形下面那条先,把所有和其交叉的竖线更新进线段树,然后扫描先向上更新,遇到竖线上端点就在线段树里删掉,遇到横线就更新答案*/#include<bits/stdc++.h> ...