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. Java中的wait和sleep

    sleep()和wait() 首先,Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 这种机制决定了,对于同一对象的多线程访问,必 ...

  2. 利用opencv进行相机标定程序

    #include "Stafx.h" ; //棋盘上有13个格子,那么角点的数目12 ; ; //图片的总张数 int main(int argc, char** argv) { ...

  3. Arduino101学习笔记(十二)—— 101定时器中断

    一.API 1.开定时器中断 //*********************************************************************************** ...

  4. thinkphp数据表操作恐怖事件。

    1当使用thinkphp的where(array())时,如果里面的字段在数据库是没有的,则默认这个条件为1,这时就可能出现大批修改记录问题.如修改所有用户的密码.特别要注意的是,这里的表字段是区分大 ...

  5. dubbo服务治理框架设计

    dubbo.JSF作为使用最广泛的服务端框治理架,其设计和实现思想值得进行学习研究. 整个服务管理框架核心的原理基于反射以及socket调用实现,服务管理框架包含服务的注册管理 服务的索引管理以及服务 ...

  6. Scau 10327 Biggest Square

    时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description You are given a M*M cloth wi ...

  7. configSections

         由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层.       这个框架的web.config文件里出现了configS ...

  8. BestCoder Round #66 (div.2)

    构造 1002 GTW likes gt 题意:中文题面 分析:照着题解做的,我们可以倒着做,记一下最大值,如果遇到了修改操作,就把最大值减1,然后判断一下这个人会不会被消灭掉,然后再更新一下最大值. ...

  9. 利用TaskCompletionSource将EAP转换成TAP

        1.原始的异步方法的调用   我们来看个简单的例子,在这里演示调用 WebClient.DownloadStringAsync 方法(这个方法不是 TAP),然后由 WebClient.Dow ...

  10. HTML DOM domain 属性

    定义和用法 domain 属性可返回下载当前文档的服务器域名. 语法 document.domain 说明 该属性是一个只读的字符串,包含了载入当前文档的 web 服务器的主机名. 提示和注释 提示: ...