lintcode-501-迷你推特
501-迷你推特
实现一个迷你的推特,支持下列几种方法
- postTweet(user_id, tweet_text). 发布一条推特.
- getTimeline(user_id). 获得给定用户最新发布的十条推特,按照发布时间从最近的到之前排序
- getNewsFeed(user_id). 获得给定用户的朋友或者他自己发布的最新十条推特,从发布时间最近到之前排序
- follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
- 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-迷你推特的更多相关文章
- [LintCode] Mini Twitter 迷你推特
Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- [原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推)
[原创]上海好买基金招高级Java技术经理/运维主管/高级无线客户端开发等职位(内推) 内部推荐职位 高级JAVA技术经理: 岗位职责: 负责项目管理(技术方向),按照产品开发流 ,带领研发团队,制定 ...
- 迷你 MVC
深入研究 蒋金楠(Artech)老师的 MiniMvc(迷你 MVC),看看 MVC 内部到底是如何运行的 2014-04-05 13:52 by 自由的生活, 645 阅读, 2 评论, 收藏, 编 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- 学生党如何拿到阿里技术offer: 《2016阿里巴巴校招内推offer之Java研发工程师(成功)》
摘要: 这篇文章字字珠玑,这位面试的学长并非计算机相关专业,但是其技术功底足以使很多计算机专业的学生汗颜,这篇文章值得我们仔细品读,其逻辑条理清晰,问题把握透彻,语言表达精炼,为我们提供了宝贵的学习经 ...
随机推荐
- 大数据学习--day17(Map--HashMap--TreeMap、红黑树)
Map--HashMap--TreeMap--红黑树 Map:三种遍历方式 HashMap:拉链法.用哈希函数计算出int值. 用桶的思想去存储元素.桶里的元素用链表串起来,之后长了的话转红黑树. T ...
- 嵌入式Linux系统移植——uboot常用命令
flash的一般分区: 其它数据 环境变量 可执行程序.如bootloader print(可缩写为:pri):打印查看uboot这个软件中集成的环境变量setenv.saveenv:设置.保存环境变 ...
- Xilinx FPGA高速串行收发器简介
1 Overview 与传统的并行实现方法相比,基于串行I/O的设计具有很多优势,包括:器件引脚数较少.降低了板空间要求.印刷电路板(PCB)层数较少.可以轻松实现PCB设计.连接器较小.电磁干扰降低 ...
- Java程序设计 第16周 课堂实践 —— 数据库4
Java程序设计 第16周 课堂实践 -- 数据库4 课堂实践任务4 查询world数据库,查询哪个国家的平均寿命最长. 代码分析 实现查询数据库需要我们修改Message.java,MessageD ...
- 2016-2017-2 20155322 实验三 敏捷开发与XP实践
2016-2017-2 20155322 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验知识点 敏捷开发(Agile Development)是一种以人为核心.迭代.循序 ...
- day 3 局部变量 全局变量
1.局部变量 2.全局变量(死歌的大招)函数前面声明的都是全局变量 3.全局变量和局部变量的区别 1)老方法 def get_temper(): temper = 33 return temper d ...
- SRM 698 div1 RepeatString
250pts RepeatString 题意:问最少修改多少次将一个字符串修改为AA的形式.可以插入一个字符,删除一个字符,修改字符. 思路:枚举分界点,然后dp一下. /* * @Author: m ...
- Mysql:存储过程游标不进循环的原因详解
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客给刚接触存储过程的朋友做个引导作用,目的是解决游标不走循环 很多人发现他的游标,无论是嵌套循环还是单层 ...
- Linux中如何安装Apache服务器
Linux中如何安装Apache服务器 由于学习的需要,所有手动安装了一下Apache源码包,安装过程中的问题千奇百怪,但是如果弄清楚了问题出在哪里,那么也不是太难.如果有学习者出现安装中的问题,可仔 ...
- Lua学习笔记(3):运算符
算术运算符 运算符 描述 + 加法运算符 - 减法运算符 * 乘法运算符 / 除法运算符 % 取模运算符 ^ 乘幂 A=3 print(A^2)输出9 关系运算符 ~= 不等于 == 等于 > ...