题目连接

https://leetcode.com/problems/design-twitter

Design Twitte

Description

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.

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);

根据题意直接模拟即可, 
我用了一个固定大小的set来保存10个最近的id 
如果tweets少于10个直接插入即可,否则每插入一个就与set的最后一个元素判断 
(已重载比较函数,set里面的元素是以时间戳从大到小排序的)

PS:好久都没有更新blogs了,太懒了(⊙﹏⊙)b

class Twitter {
private:
struct P {
int id, ref;
P(int i = 0, int j = 0) :id(i), ref(j) {}
inline bool operator<(const P &a) const { return ref > a.ref; }
};
public:
Twitter() { time = 1; }
void postTweet(int userId, int tweetId) {
userPost[userId].insert(P(tweetId, time++));
userFollow[userId].insert(userId);
}
vector<int> getNewsFeed(int userId) {
q.clear();
vector<int> res;
for(auto &r: userFollow[userId]) {
int n = userPost[r].size();
auto it = userPost[r].begin();
n = min(n, 10);
while(n--) {
if(q.size() < 10) {
q.insert(*it++);
} else {
auto c = q.end();
if(*it < *--c) {
q.erase(c);
q.insert(*it++);
}
}
}
}
for(auto &r: q) res.push_back(r.id);
return res;
}
void follow(int followerId, int followeeId) {
userFollow[followerId].insert(followeeId);
}
void unfollow(int followerId, int followeeId) {
if(followerId == followeeId) return;
userFollow[followerId].erase(followeeId);
}
private:
int time;
set<P> q;
unordered_map<int, set<P>> userPost;
unordered_map<int, set<int>> userFollow;
};

leetcode 355 Design Twitte的更多相关文章

  1. leetcode@ [355] Design Twitter (Object Oriented Programming)

    https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...

  2. [LeetCode] 355. Design Twitter 设计推特

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...

  3. [leetcode]355. Design Twitter设计实现一个微博系统

    //先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...

  4. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

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

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

  7. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  8. LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  9. #Leetcode# 707. Design Linked List

    https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...

随机推荐

  1. Python + winpcap抓包和发包

    winpcapy Python的winpcapy库可以简单地实现收发Layer2层(数据链路层,以太网)数据. winpcapy主页:https://github.com/orweis/winpcap ...

  2. Spring JdbcTemplate+JdbcDaoSupport实例(和比较)

    首先,数据库是这样的,很简单. 当然,要引入spring的包,这里我全部导入了,省事. applicationContext.xml是这样的: <?xml version="1.0&q ...

  3. 面试问题 - SQL 中存储过程与函数的区别

    SQL 中的存储过程与函数没有本质上的区别 函数 -> 只能返回一个变量. 函数可以嵌入到sql中使用, 可以在select 中调用, 而存储过程不行.  但函数也有着更多的限制,比如不能使用临 ...

  4. [转载]Java程序员掌握的10大项知识体系--精通太难说出口

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...

  5. Java中编写线程安全代码的原理(Java concurrent in practice的快速要点)

    Java concurrent in practice是一本好书,不过太繁冗.本文主要简述第一部分的内容. 多线程 优势 与单线程相比,可以利用多核的能力; 可以方便的建模成一个线程处理一种任务; 与 ...

  6. C#识别图中二维码

    1.在NuGet中添加 ZXing.Net 2.实例代码 /// <summary> /// 识别图中二维码 /// </summary> /// <param name ...

  7. iOS回顾笔记( 01 )-- XIB和纯代码创建应用的对比

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  8. click点击事件先后顺序的问题

    //页面加载时,每秒钟调用一次var times = setInterval("loadFlws()","1000"); function loadFlws() ...

  9. thinkphp5.1页面页面模板及参数配置

    success和error跳转的模板在thinkphp/tpl/dispatch_jump.tpl 配置参数在thinkphp\library\traits\controller\jump.php文件 ...

  10. 因为.patch_storage目录丢失,导致opatch打补丁失败

    一套新装的ORACLE Restart环境(11.2.0.3.0),计划最新的PSU,在使用opath auto方式安装补丁时报错,表面上的错误信息提示opatch工具不满足版本要求: [root@d ...