leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:
- postTweet(userId, tweetId): Compose a new tweet.
- getNewsFeed(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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1); // User 1 follows user 2.
twitter.follow(1, 2); // User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6); // User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1); // User 1 unfollows user 2.
twitter.unfollow(1, 2); // User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);
Example
public class Twitter {
class pair {
public int userId;
public int tweetId;
public pair(int userId, int tweetId) {
this.userId = userId;
this.tweetId = tweetId;
}
}
public ArrayList<pair> tweetList = null;
public HashMap<Integer, HashSet<Integer> > followList = null;
/** Initialize your data structure here. */
public Twitter() {
tweetList = new ArrayList<pair> ();
followList = new HashMap<Integer, HashSet<Integer> > ();
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
pair p = new pair(userId, tweetId);
tweetList.add(p);
}
/** 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> rs = new ArrayList<Integer> ();
if(!followList.containsKey(userId)) {
followList.put(userId, new HashSet<Integer> ());
}
HashSet<Integer> network = followList.get(userId);
int size = tweetList.size();
for(int i=size-1, t=10; i>=0 && t>0; --i) {
pair p = tweetList.get(i);
//System.out.println(p.userId);
if(p.userId == userId || network.contains(p.userId)) {
rs.add(p.tweetId);
--t;
}
}
return rs;
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
if(!followList.containsKey(followerId)) {
followList.put(followerId, new HashSet<Integer> ());
}
followList.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(followList.containsKey(followerId)) {
followList.get(followerId).remove(followeeId);
}
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/
leetcode@ [355] Design Twitter (Object Oriented Programming)的更多相关文章
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- [leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容) class TwitterData { int userId; int twitterId; public Tw ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
- 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性
一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 355 Design Twitter 设计推特
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(userI ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- leetcode 355 Design Twitte
题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...
随机推荐
- 223. Rectangle Area
题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...
- checkbox 全选、全不选、反选 插件
jquery.checkbox.js: ;(function($,window,document,undefined){ $.fn.check=function(mode){ mode= mode | ...
- Android app Splash页的替代方案
一般的App想要显示公司的log什么的,都会在启动的第一个页面显示,就是SplashActivity. 目前在看到一个替代SplashActivity的方案. 使用SplashActivity的时候, ...
- NodeJS常用库说明
underscore:1.合并json async:1.异步编程同步化
- 让Eclipse和NetBeans共享同一个项目
有的时候,我们会下载一些源代码来学习研究,但是下载下来的工程文件是eclipse的或者是NetBeans的.如果手头上没有eclipse或者没有 NetBeans,或只有其中一个怎么办?又或者,你习惯 ...
- HDU 1677
Nested Dolls Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- java--关键字和保留字
关键字:Java的关键字对java的编译器有特殊的意义,他们用来表示一种数据类型,或者表示程序的结构等. 保留字:为java预留的关键字.现在还没用到,但是在升级版本中可能作为关键字. 访问控制:pr ...
- UVa 10341 (二分求根) Solve It
很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. #include <cstdio> #include <cmath> //using namespa ...
- eclipse无法与手机连上的解决方案
在eclipse上开发android应用,有时候会遇到eclipse无法识别手机的问题,就算把数据线拔了又插,插了又拔,哪怕是重启eclipse 甚至是重启电脑,这个问题也依然是解决不了.这时候就非常 ...
- UVA 11806 Cheerleaders (容斥原理)
题意 一个n*m的区域内,放k个啦啦队员,第一行,最后一行,第一列,最后一列一定要放,一共有多少种方法. 思路 设A1表示第一行放,A2表示最后一行放,A3表示第一列放,A4表示最后一列放,则要求|A ...