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; } ...
随机推荐
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- [学习笔记]设计模式之Decorator
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Decorator(装饰)模式,可以动态地给一个对象添加一些额外的职能.为了更好地理解这个模式,我们将时间线拉回Bridge模式笔记的 ...
- Java学习----日期函数
Date Calendar public class TestDate { private Date date; Calendar calendar; public TestDate() { // ...
- React 学习资源分享 菜鸟刚学5天 博客写的不多 不懂写博客的套路
http://www.ruanyifeng.com/blog/2015/03/react.html 首先个人强烈推荐 阮一峰的React基础 细细过一遍,看得出大师的用心良苦 然后就开始看书般的过ht ...
- 完美方案——iOS的WebView自适应内容高度
/////////////////////////////初始化,self.view是父控件///////////////////////////////// _webView = [[UIWebVi ...
- LightOj_1030 Discovering Gold
题目链接 题意: 在一个1 X N 的格子上, 每个格子都有一定的黄金, 你从第一个格子出发, 问到最后一个格子得到黄金的期望. 每次前进使用骰子投点来决定前进步数, 如果投出的点前进后会超过N, 那 ...
- oracle技巧-持续更新
1. 登录oracle数据库,执行select status from v$encryption_wallet,如果返回OPEN,表示钱夹已自动打开. 2.
- [BZOJ 1115] [POI2009] 石子游戏Kam 【阶梯博弈】
题目链接:BZOJ - 1115 题目分析 首先看一下阶梯博弈: 阶梯博弈是指:初始有 n 堆石子,每次可以从任意的第 i 堆拿若干石子放到第 i - 1 堆.最终不能操作的人失败. 解法:将奇数位的 ...
- iOS便捷开发工具分享
项目/代码优化工具 1.objec_dep,可以了解项目中各个类的关联信息,了解项目中无效文件,知道双向应用的文件. 下载地址: https://github.com/nst/objc_dep 2.b ...
- 【Windows 8】pid为4的system进程占用80端口的解决办法
因为Apache无法启动的原因,用netstat命令查看了一下80端口是否被占用了,如下: C:\Users\Maple>netstat -ano | findstr TCP LISTENING ...