leetcode 355. Design Twitter
来源:互联网 发布:linux测试端口命令 编辑:程序博客网 时间:2024/04/28 22:43
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.
Example:
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);
这种题不喜欢,还是直接看代码吧!
代码如下:
/* * 这种题不喜欢 **/public class Twitter { class TwitterNode { int userId; int twitterId; TwitterNode(int userId,int twitterId) { this.userId = userId; this.twitterId = twitterId; } } List<TwitterNode> twitterList; Map<Integer,Set> userManager; /** Initialize your data structure here. */ public Twitter() { twitterList = new LinkedList<TwitterNode>(); userManager = new HashMap<Integer,Set>(); } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { if(!userManager.containsKey(userId)) { Set<Integer> tmp = new HashSet<>(); tmp.add(userId); userManager.put(userId,tmp); } TwitterNode node = new TwitterNode(userId,tweetId); twitterList.add(0,node); } /** 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 LinkedList<>(); if(userManager.containsKey(userId)) { Set<Integer> follows = userManager.get(userId); int count = 0; for(TwitterNode curNode:twitterList) { if(count == 10) break; if(follows.contains(curNode.userId)) { res.add(curNode.twitterId); count++; } } } return res; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { Set<Integer> focusSet; if(!userManager.containsKey(followeeId)) {//必须包含被关注者 focusSet = new HashSet<>(); focusSet.add(followeeId); userManager.put(followeeId,focusSet); } if(!userManager.containsKey(followerId)) { focusSet = new HashSet<>(); focusSet.add(followerId); userManager.put(followerId,focusSet); } userManager.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 && userManager.containsKey(followerId)) { if(userManager.containsKey(followeeId)) { userManager.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
- 【LeetCode】355. Design Twitter
- leetcode.355. Design Twitter
- [leetcode]355. Design Twitter
- Leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- leetcode 355. Design Twitter
- LeetCode 355. Design Twitter(设计Twitter)
- LeetCode—355. Design Twitter
- leetcode题解-355. Design Twitter
- [leetcode] 355. Design Twitter 解题报告
- Leetcode 355. Design Twitter (Medium) (cpp)
- [Leetcode] 355. Design Twitter 解题报告
- Leetcode 355. Design Twitter 实现一个Twitter系统 解题报告
- 【Leetcode】Design Twitter
- [LeetCode] Design Twitter
- LeetCode Design Twitter
- App installation failed.could not write to the device.
- MVP模式的简单介绍跟简单实现登录功能
- ROS09-ROS库-可视化
- 登录后跳回到上一次浏览的页面
- Oil Skimming HDU
- leetcode 355. Design Twitter
- 面试_简历
- 集合的排序
- micro office 2016 的下载安装
- Servlet(一)
- flash消息
- 组件化思想之图片加载库picasso的用法及封装
- MQTT进阶篇
- ubuntu下利用MATLAB将c/cpp文件转化成mexa64文件