• leetcode 355 设计推特


    用链表存储用户发送的每一个推特,用堆获取最先的10条动态

    1. class Twitter {
    2. Map> followMap;
    3. //规定最新的放到最后
    4. Map postMap;
    5. //优先队列(堆)
    6. PriorityQueue priorityQueue;
    7. int timeStamp = 0;
    8. int limit = 10;
    9. public Twitter() {
    10. followMap = new HashMap();
    11. postMap = new HashMap<>();
    12. //按照每一个推特发送的时间戳由大到小排布
    13. priorityQueue = new PriorityQueue<>((t1,t2) -> t2.timeStamp - t1.timeStamp);
    14. }
    15. //userId发送推特
    16. public void postTweet(int userId, int tweetId) {
    17. //首先根据postMap来获取userId对应发送到文章
    18. Tweet tweet = postMap.get(userId);
    19. //生成新的tweet
    20. Tweet newTweet = new Tweet(tweetId, timeStamp++, tweet);
    21. postMap.put(userId,newTweet);
    22. }
    23. //根据userId获得自己和关注用户的10条推特,按时间顺序由近到远排序
    24. public List getNewsFeed(int userId) {
    25. //因为每一个用户都有自己的优先队列,所以先清空优先队列
    26. priorityQueue.clear();
    27. //将自己和关注的用户发送的最新的推特id先放入到优先队列
    28. if (postMap.containsKey(userId))
    29. priorityQueue.offer(postMap.get(userId));
    30. Set follows = followMap.get(userId);
    31. if (follows != null){
    32. for (Integer follow : follows) {
    33. if (postMap.containsKey(follow))
    34. priorityQueue.offer(postMap.get(follow));
    35. }
    36. }
    37. //现在用户和所有关注的推特都已经放入到优先队列,开始获取前10条
    38. int count = 0;
    39. ArrayList result = new ArrayList<>();
    40. while (!priorityQueue.isEmpty() && count < limit){
    41. //获取头部,在优先队列中删除
    42. Tweet tweet = priorityQueue.poll();
    43. result.add(tweet.id);
    44. if (tweet.next != null)
    45. priorityQueue.offer(tweet.next);
    46. count++;
    47. }
    48. return result;
    49. }
    50. //关注
    51. public void follow(int followerId, int followeeId) {
    52. // 被关注人不能是自己
    53. if (followeeId == followerId) {
    54. return;
    55. }
    56. Set follows = followMap.getOrDefault(followerId, new HashSet<>());
    57. follows.add(followeeId);
    58. followMap.put(followerId,follows);
    59. }
    60. //取关
    61. public void unfollow(int followerId, int followeeId) {
    62. // 被关注人不能是自己
    63. if (followeeId == followerId) {
    64. return;
    65. }
    66. Set follows = followMap.getOrDefault(followerId, new HashSet<>());
    67. follows.remove(followeeId);
    68. followMap.put(followerId,follows);
    69. }
    70. }
    71. class Tweet{
    72. int id;
    73. int timeStamp;
    74. Tweet next;
    75. public Tweet(int id, int timeStamp) {
    76. this.id = id;
    77. this.timeStamp = timeStamp;
    78. }
    79. public Tweet(int id, int timeStamp, Tweet next) {
    80. this.id = id;
    81. this.timeStamp = timeStamp;
    82. this.next = next;
    83. }
    84. }
    85. /**
    86. * Your Twitter object will be instantiated and called as such:
    87. * Twitter obj = new Twitter();
    88. * obj.postTweet(userId,tweetId);
    89. * List param_2 = obj.getNewsFeed(userId);
    90. * obj.follow(followerId,followeeId);
    91. * obj.unfollow(followerId,followeeId);
    92. */

  • 相关阅读:
    金融行业分布式数据库选型及实践经验
    【LeetCode:108. 将有序数组转换为二叉搜索树 + 二叉树+递归】
    Building a Robust Data Infrastructure for Cloud Computing Platforms
    全连接网络实现回归【房价预测的数据】
    java线程池并发实现代码模板
    原生js实现轮播图及无缝滚动
    MySQL 事件调度
    威固新能源GO野 伊士曼旗下品牌威固加速布局新能源车后市场
    ESP32 Arduino实战协议篇-BLE 客户端实现温度和湿度数据传输
    norflash 中块和扇区的关系
  • 原文地址:https://blog.csdn.net/hanhanhanha1/article/details/132448929