355. Design Twitter
二刷尝试了别的办法,用MAP<Integer,set>代表关注列表。
然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己。。
但是这样做超时了。
问题在于这个题解法太多,有很多不同的情况。
STACK记录所有推,然后找10个,那太难了,可能某个用户关注的只是几亿里面的某些人,搜索起来很要命。
那可以某人发推,就给所有关注他的人推一下。A关注B的时候,A刷新自己的推特队列,按时间顺序重新查找。这样比较符合现状,但是假如我有几亿关注者,每发一个就要刷新所有人的列表,这个还好。但是,每多一个人关注我,他的列表就要重新构建,按照时间,那就要遍历他关注的所有人的所有推,也很要命。。
另外数组初始问题在现实不需要纠结。。谁注册给谁ID就行了。。。实际中不会存在后台接到了没注册ID账户的操作请求。。。
总之。。这个题就是各种tradeoff,给的限定条件越多越容易接近最好设计方案。如果面试,希望是面试官给出设计,自己能距离说出这种设计的利弊,否则基本给个设计方案都能指出不适用的条件。
public class Twitter
{
Stack<Integer> tweets = new Stack<>();
Map<Integer,Integer> tweetsMap = new HashMap<Integer,Integer>();
List<Set<Integer>> users = new ArrayList<Set<Integer>>();
/** Initialize your data structure here. */
public Twitter()
{
for(int i = 0; i < 1000;i++)
{
Set<Integer> tempSet = new HashSet<Integer>(1000);
tempSet.add(i);
users.add(new HashSet<Integer>(tempSet));
}
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId)
{
tweets.push(tweetId);
tweetsMap.put(tweetId,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. */
public List<Integer> getNewsFeed(int userId)
{
List<Integer> res = new ArrayList<>();
Stack<Integer> tempStk = new Stack<Integer>();
while(res.size() < 10)
{
if(tweets.size()==0) break;
int temptweetID = tweets.pop();
tempStk.push(temptweetID);
if(users.get(userId).contains(tweetsMap.get(temptweetID))) res.add(temptweetID);
}
while(!tempStk.isEmpty())
{
tweets.push(tempStk.pop());
}
return res;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId)
{
users.get(followerId).add(followeeId);
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId)
{
if(followerId != followeeId)
users.get(followerId).remove(followeeId);
}
}
这个初始4000人的就很蠢。。自己都想抽自己。假如有10个用户就浪费了,有4001个用户就报错了。
但是不初始4000人,每次有相关用户活动都要检测是否存在,不存在就加超时了。其实我就想知道,没注册的用户哪来的ID,怎么去给服务器发操作请求。
355. Design Twitter的更多相关文章
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 355 Design Twitter 设计推特
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(userI ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- leetcode 355 Design Twitte
题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...
- LeetCode "Design Twitter"
A mix of hashmap, list and heap. struct Tw { Tw(long long pts, int tid) { ts = pts; tweetid = tid; } ...
随机推荐
- Android运行异常情况分析(持续更新)
1.java.lang.IllegalAccessException: access to class not allowed 原因:在写class 文件的时候没有把class设置成public 2. ...
- 转载:Python正则表达式
原文在 http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python ...
- 站点下的GridView的RowCommand事件的设置,与站点应用不一样
<ItemTemplate> <%--<a ...
- Apache 支持PHP
①加载PHP模块到Apache中: LoadModule php5_module "d:\php5\php5apache2_2.dll" ②加入识别扩展名为.php文件(也可以 ...
- [WinJS] Promise 用法
初学 WinJS, 可能对 Promise 的用法不大清楚,本文简要说明一下 WinJS中 promise 的基本用法. 主要参考自:http://try.buildwinjs.com/#promis ...
- iOS 中KVC、KVO、NSNotification、delegate 总结及区别-b
1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的 ...
- css3:user-select属性
一.user-select简介 这是在css3 UI规范中新增的一个功能,用来控制内容的可选择性 二.user-select:值 auto——默认值,用户可以选中元素中的内容 none——用户不能选择 ...
- Contest 20140923 登月计划 BabyStepGaintStep
登月计划 查看 提交 统计 提问 总时间限制: 40000ms 内存限制: 256000kB 描述 HJA在和学弟学数学,于是便有了一道非常简单的数学题:求满足 的最小自然数x. 输入 输入数据一 ...
- Contest20140710 eagleeggs
eagleeggs|eagleeggs.in|eagleeggs.out 题目描述: 共有N个硬度相同的鹰蛋,硬度是一个整数(并且已知其不大于H),表示这个蛋从天上掉下来不摔碎的最大高度.为了找出这个 ...
- 浅谈C# .Net技术面试 , 正在找工作的人一定要看看
1.引子 最近一直在负责.net(B/S方向)技术面试相关的工作,前前后后面试了不少人,但是通过率较低,大概只有20%左右:有颇多感慨. 最近也一直比较困惑,原因究竟是什么? 是我们要求太高,应聘者本 ...