Implement a simple twitter. Support the following method:

postTweet(user_id, tweet_text). Post a tweet.
getTimeline(user_id). Get the given user's most recently 10 tweets posted by himself, order by timestamp from most recent to least recent.
getNewsFeed(user_id). Get the given user's most recently 10 tweets in his news feed (posted by his friends and himself). Order by timestamp from most recent to least recent.
follow(from_user_id, to_user_id). from_user_id followed to_user_id.
unfollow(from_user_id, to_user_id). from_user_id unfollowed to to_user_id.

Example
postTweet(1, "LintCode is Good!!!")
>> 1
getNewsFeed(1)
>> [1]
getTimeline(1)
>> [1]
follow(2, 1)
getNewsFeed(2)
>> [1]
unfollow(2, 1)
getNewsFeed(2)
>> []

这道题让我们实现一个迷你推特,具有发布消息,获得时间线,新鲜事,加关注和取消关注等功能,其中获得用户的时间线是返回最新10条推特,而新鲜事是返回最新10条自己的和好友的推特,如果取消关注了,那么返回的新鲜事中就没有取消关注的好友的推特。这是一道蛮有意思的设计题,我们为了简化问题,不会真的去获取系统时间来给推特排序,而是我们使用一个变量order,每发布一条消息,order自增1,这样我们就知道order大的发布的就晚,我们新建一个结构体Node,用来给每个tweet绑定一个order,然后我们写一个从一个Node数组中返回最后10个Node的函数,和一个从Node数组中返回前10个Node的函数,然后我们还需要两个哈希表,一个用来建立每个用户和其所有好友之间的映射,另一个用来建立每个用户和其发布的所有推特之间的映射,另外我们还需要一个变量order来记录发布推特的顺序。

对于postTweet函数,我们首先利用Tweet类提供的create函数建立一个tweet,然后我们看发布者是否在users_tweets里,如果不在添加这个用户,然后将这条推特加到和其映射的数组中,最后返回tweet。

对于getTimeline函数,我们先从该用户的推特集中返回最新的10条推特,然后按时间先后顺序排序,然后再返回即可。

对于getNewsFeed函数,我们先把该用户的推特集中最新10条保存下来,然后遍历其所有的好友,将其好友的最新10条保存下来,然后整个按时间先后顺序排序,返回最新10条即可。

对于follow函数,我们将好友加入用户的好友表里。

对于unfollow函数,我们将好友从用户的好友表里删除。

class MiniTwitter {
public:
struct Node {
int order;
Tweet tweet;
Node(int o, Tweet t): order(o), tweet(t){}
}; vector<Node> getLastTen(vector<Node> t) {
int last = ;
if (t.size() < ) last = t.size();
return vector<Node>(t.end() - last, t.end());
} vector<Node> getFirstTen(vector<Node> t) {
int last = ;
if (t.size() < ) last = t.size();
return vector<Node>(t.begin(), t.begin() + last);
} MiniTwitter() {
order = ;
} // @param user_id an integer
// @param tweet a string
// return a tweet
Tweet postTweet(int user_id, string tweet_text) {
Tweet tweet = Tweet::create(user_id, tweet_text);
if (!users_tweets.count(user_id)) users_tweets[user_id] = {};
++order;
users_tweets[user_id].push_back(Node(order, tweet));
return tweet;
} // @param user_id an integer
// return a list of 10 new feeds recently
// and sort by timeline
vector<Tweet> getNewsFeed(int user_id) {
vector<Node> t;
if (users_tweets.count(user_id)) {
t = getLastTen(users_tweets[user_id]);
}
if (friends.count(user_id)) {
for (auto it : friends[user_id]) {
if (users_tweets.count(it)) {
vector<Node> v = getLastTen(users_tweets[it]);
t.insert(t.end(), v.begin(), v.end());
}
}
}
sort(t.begin(), t.end(), [](const Node &a, const Node &b){return a.order > b.order;});
vector<Tweet> res;
t = getFirstTen(t);
for (auto a : t) {
res.push_back(a.tweet);
}
return res;
} // @param user_id an integer
// return a list of 10 new posts recently
// and sort by timeline
vector<Tweet> getTimeline(int user_id) {
vector<Node> t;
if (users_tweets.count(user_id)) {
t = getLastTen(users_tweets[user_id]);
}
sort(t.begin(), t.end(), [](const Node &a, const Node &b){return a.order > b.order;});
vector<Tweet> res;
t = getFirstTen(t);
for (auto a : t) {
res.push_back(a.tweet);
}
return res;
} // @param from_user_id an integer
// @param to_user_id an integer
// from user_id follows to_user_id
void follow(int from_user_id, int to_user_id) {
friends[from_user_id].insert(to_user_id);
} // @param from_user_id an integer
// @param to_user_id an integer
// from user_id unfollows to_user_id
void unfollow(int from_user_id, int to_user_id) {
friends[from_user_id].erase(to_user_id);
} private:
unordered_map<int, set<int>> friends;
unordered_map<int, vector<Node>> users_tweets;
int order;
};

参考资料:

http://www.jiuzhang.com/solutions/mini-twitter/

[LintCode] Mini Twitter 迷你推特的更多相关文章

  1. [LeetCode] Design Twitter 设计推特

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

  2. 355 Design Twitter 设计推特

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能:    postTweet(userI ...

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

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

  4. [LeetCode] Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

  5. Python 提取Twitter转发推文的元素(比方username)

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-24 @author: guaguastd @name: e ...

  6. Mini Twitter

    Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...

  7. 385 Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...

  8. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  9. memory prefix mini mono multi out _m 5

      1● mini 小 迷你   2● mono 单一 ,单   3● multi 多

随机推荐

  1. Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强

    本文主要介绍一个支持图片自动预取.支持多种缓存算法的图片缓存的使用及功能.图片较大需要SD卡保存情况推荐使用ImageSDCardCache. 与Android LruCache相比主要特性:(1). ...

  2. java中的三种取整函数

        舍掉小数取整:Math.floor(3.5)=3 四舍五入取整:Math.rint(3.5)=4 进位取整:Math.ceil(3.1)=4

  3. hdu 3037 Saving Beans Lucas定理

    Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. Html 块级元素和行级元素

    转载:http://blog.csdn.net/yuyanqiao/article/details/8558118 内联元素(inline element)* a - 锚点* abbr - 缩写* a ...

  5. 基于jdk1.7实现的excel导出工具类

    通用excel导出工具类,基于泛型.反射.hashmap 以及基于泛型.反射.bean两种方式 import java.io.*;import java.lang.reflect.Field;impo ...

  6. maven junit 单元测试插件配置

    单元测试插件配置 pom.xml中增加 <dependency> <groupId>junit</groupId> <artifactId>junit& ...

  7. CocoaPods安装和使用教程

    Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...

  8. EBay .Net SDK Api 实践

        1.请求流程介绍   提供SOA地址:https://api.ebay.com/wsapi   WSDL生成的代码在WebService.cs文件当中.   ApiCall封装了所有的RPC, ...

  9. HTML无序列表和有序列表

    html无序列表<ul><li></li></ul>   ul属性设定:<ul type="square"> 常用属性值 ...

  10. SPOJ TSUM Triple Sums(FFT + 容斥)

    题目 Source http://www.spoj.com/problems/TSUM/ Description You're given a sequence s of N distinct int ...