501-迷你推特

实现一个迷你的推特,支持下列几种方法

  1. postTweet(user_id, tweet_text). 发布一条推特.
  2. getTimeline(user_id). 获得给定用户最新发布的十条推特,按照发布时间从最近的到之前排序
  3. getNewsFeed(user_id). 获得给定用户的朋友或者他自己发布的最新十条推特,从发布时间最近到之前排序
  4. follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
  5. unfollow(from_user_id, to_user_id). from_user_id 取消关注 to_user_id.

样例

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

思路

  • 使用数组记录所有 twitter 的信息,数组值的插入顺序就是所有用户新建 Twitter 的顺序,所以在寻找最新的 twitter 时,只需要从后向前遍历数组即可
  • 在寻找用户自己的 twitter 时,从后向前遍历数组,若此条 twitter 的发布用户是自己,即可以将此 twitter 加入返回列表
  • 在寻找用户关注的用户(包括用户自己)的 twitter 时,使用 map 记录某用户的关注用户列表(map<int, set>),从后向前遍历数组,若此条 twitter 的发布用户在此用户的关注列表中,即可以将此 twitter 加入返回列表
  • 关注用户就是在此用户的关注列表中新建一个关注信息,取关就是取消关注信息

code

/**
* Definition of Tweet:
* class Tweet {
* public:
* int id;
* int user_id;
* String text;
* static Tweet create(int user_id, string tweet_text) {
* // This will create a new tweet object,
* // and auto fill id
* }
* }
*/
class MiniTwitter {
private:
vector<Tweet> Tweets;
map<int, set<int>> follows; public:
MiniTwitter() {
// initialize your data structure here.
} // @param user_id an integer
// @param tweet a string
// return a tweet
Tweet postTweet(int user_id, string tweet_text) {
// Write your code here
// 将用户自身加入其关注列表
follows[user_id].insert(user_id);
// 新建 twitter
Tweet t = Tweet::create(user_id, tweet_text);
Tweets.push_back(t);
return t;
} // @param user_id an integer
// return a list of 10 new feeds recently
// and sort by timeline
vector<Tweet> getNewsFeed(int user_id) {
// Write your code here
vector<Tweet> result;
// 在总 twitter 表中查找用户自己发布的 Twitter,最多十条
for (int i = Tweets.size() - 1, count = 0; i >= 0 && count < 10; i--) {
if (follows[user_id].find(Tweets[i].user_id) != follows[user_id].end()) {
result.push_back(Tweets[i]);
count++;
}
}
return result;
} // @param user_id an integer
// return a list of 10 new posts recently
// and sort by timeline
vector<Tweet> getTimeline(int user_id) {
// Write your code here
vector<Tweet> result;
// 在总 twitter 表中查找用户关注的用户发布的 Twitter,最多十条
for (int i = Tweets.size() - 1, count = 0; i >= 0 && count < 10; i--) {
if (Tweets[i].user_id == user_id) {
result.push_back(Tweets[i]);
count++;
}
}
return result;
} // @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) {
// Write your code here
// 新建关注关系
follows[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) {
// Write your code here
// 取消关注关系
follows[from_user_id].erase(to_user_id);
}
};

lintcode-501-迷你推特的更多相关文章

  1. [LintCode] Mini Twitter 迷你推特

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

  2. [LintCode]——目录

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

  3. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  4. [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推)

    [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推) 内部推荐职位 高级JAVA技术经理: 岗位职责: 负责项目管理(技术方向),按照产品开发流 ,带领研发团队,制定 ...

  5. 迷你 MVC

    深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的 2014-04-05 13:52 by 自由的生活, 645 阅读, 2 评论, 收藏, 编 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  9. 学生党如何拿到阿里技术offer: 《2016阿里巴巴校招内推offer之Java研发工程师(成功)》

    摘要: 这篇文章字字珠玑,这位面试的学长并非计算机相关专业,但是其技术功底足以使很多计算机专业的学生汗颜,这篇文章值得我们仔细品读,其逻辑条理清晰,问题把握透彻,语言表达精炼,为我们提供了宝贵的学习经 ...

随机推荐

  1. string函数库的原型

    #ifndef __HAVE_ARCH_STRCPY /** * strcpy - Copy a %NUL terminated string * @dest: Where to copy the s ...

  2. 01-Python简介

    人生苦短,我用 Python —— Life is short, you need Python 目标 Python 的起源 Python 解释器 是用 C 语言实现的,并能够调用 C 语言的库文件. ...

  3. Python--通过索引excel表将文件进行文件夹分类的脚本+读取指定目录下所有文件名的脚本

    1.通过索引excel表将文件进行文件夹分类的脚本,此脚本由于将ip和id对应并生成对应id的文件夹将文件进行分类,也可以任意规定表格内容,通过vul_sc_ip.txt和xlsx文件进行索引. # ...

  4. Java ThreadLocal 源代码分析

    Java ThreadLocal 之前在写SSM项目的时候使用过一个叫PageHelper的插件 可以自动完成分页而不用手动写SQL limit 用起来大概是这样的 最开始的时候觉得很困惑,因为直接使 ...

  5. R语言学习笔记(二十一五):如何如何提升R语言运算的性能以及速度

    在R中获得快速运行代码的方法 使用向量化运算 R语言的并行计算可以用parallel和foreach包 加快R运行速度还可以使用cmpfun()函数即字节码编译器 再者就是在R中调用C或C++ 同时还 ...

  6. markupsafe._compat出错的解决办法

    在windows下用pip进行安装的flask和freeze会在运行程序的时候出现报错 markupsafe._compat出错,那么此时找到对应的pip文件夹下自己创建一个_compat.py 然后 ...

  7. Reflow & Repaint

    http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/ http://segmentfault.com/a/1190000002 ...

  8. 20155202 《Java程序设计》实验二(面向对象程序设计)实验报告

    20155202 <Java程序设计>实验二(面向对象程序设计)实验报告 代码托管 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉 ...

  9. 【LG3237】[HNOI2014]米特运输

    题面 洛谷 题解 代码 #include <iostream> #include <cstdio> #include <cstdlib> #include < ...

  10. 【CF833D】Red-Black Cobweb

    [CF833D]Red-Black Cobweb 题面 洛谷 题解 看到这种统计路径的题目当然是淀粉质啦. 考虑转化一下信息设一条路径上有红点\(a\)个,黑点\(b\)个 则\(2min(a,b)\ ...