LeetCode—355. Design Twitter
来源:互联网 发布:和平崛起 知乎 编辑:程序博客网 时间:2024/04/28 08:06
思路:建立2个List存储Twitter信息(userId和twitterId),建立一个map<Integer,Map<Integer,Integer>>存储follow信息(默认必须存储自己的,当然可以在get的时候添加自己的),剩下的就是一些细节问题,按照题目的意思来。
GitHub地址:https://github.com/corpsepiges/leetcode
点此进入
有Java版本和Python版本,求star。
public class Twitter { List<Integer> tweetList; List<Integer> userList; Map<Integer, Map<Integer,Integer>> followMap; /** Initialize your data structure here. */ public Twitter() { tweetList=new ArrayList<Integer>(); userList=new ArrayList<Integer>(); followMap=new HashMap<Integer, Map<Integer,Integer>>(); } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { if (followMap.get(userId)==null) { Map<Integer,Integer> map=new HashMap<Integer,Integer>(); map.put(userId, 1); followMap.put(userId, map); } tweetList.add(tweetId); userList.add(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> ans=new ArrayList<Integer>(); if (followMap.get(userId)==null) { return ans; } int i=tweetList.size()-1; Set<Integer> follow=followMap.get(userId).keySet(); while (i>=0&&ans.size()<10) { int id=userList.get(i); int tweet=tweetList.get(i); if (follow.contains(id)) { ans.add(tweet); } i--; } return ans; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { if (followMap.get(followerId)==null) { Map<Integer, Integer> map=new HashMap<Integer,Integer>(); map.put(followerId, 1); followMap.put(followerId, map); } followMap.get(followerId).put(followeeId, 1); } /** 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&&followMap.get(followerId)!=null&&followMap.get(followerId).get(followeeId)!=null) { followMap.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); */
0 0
- 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
- LeetCode 355. Design Twitter(设计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
- 删除前JS弹出提示
- R语言︱SNA-社会关系网络—igraph包(社群划分、画图)(三)
- Matlab安装
- CentOS 6.3下Samba服务器的安装与配置
- 【Linux】硬盘格式化--mkfs
- LeetCode—355. Design Twitter
- Android 设置状态栏后adjustResize属性无效
- gulp常用模块gulp-sass,gulp-autoprefixer,gulp-wrap,browser-sync,gulp-rev,gulp-rev-collector模块解析
- easyui formatter实现超链接效果
- Android:Activity中onCreate方法的参数及用途
- java源码分析(9)-Byte
- 手机传感器简述
- [阅读] 算法导论-代入法证明
- PC站添加终端判断代码