zoukankan      html  css  js  c++  java
  • 355. 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:

    1. postTweet(userId, tweetId): Compose a new tweet.
    2. 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.
    3. follow(followerId, followeeId): Follower follows a followee.
    4. 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);

    出现了,史上最长答案

    看看题,要返回最新的10条微博,不妨设一个全局变量时间戳timestamp

    然后对user,tweet各进行OOD设计

    user中有userid,关注列表followed以及最新一条微博tweet_head,注意初始化的时候要自己follow自己

    Tweet中有time,id(虽然没啥用),next(把一个user的微博串联起来),user的tweet_head永远是第一条微博

    关键的method是getnewsFeed,返回10条最新微博:

    先把这个user的关注列表拿出来,把所有关注者的第一条微博放入pq中,按时间decreasing排列,注意第一条微博可能是空就跳过。这样能保证pq中的peek已经是最最新微博

    接下来poll pq并加入res list中,如果poll出来的当前微博有下一条,在offer进去防止遗漏。完毕

    class Twitter {
        private static int timestamp;
        private Map<Integer, User> map;
        
        class User {
            public int userid;
            public Set<Integer> followed;
            public Tweet tweet_head;
            
            public User(int userid) {
                followed = new HashSet();
                this.userid = userid;
                follow(userid); //follow itself
                tweet_head = null;
            }
            
            public void follow(int id) {
                followed.add(id);
            }
            
            public void unfollow(int id) {
                followed.remove(id);
            }
            
            public void post(int tweetid) {
                Tweet twt = new Tweet(tweetid);
                twt.next = tweet_head;
                tweet_head = twt;
            }
        }
        
        class Tweet {
            public int time;
            public int id;
            public Tweet next;
            
            public Tweet(int id) {
                this.id = id;
                this.time = timestamp++;
                next = null;
            }
        }
        
        /** Initialize your data structure here. */
        public Twitter() {
            timestamp = 0;
            map = new HashMap();
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) { 
            if(!map.containsKey(userId)) {
                User user = new User(userId);
                map.put(userId, user);
            }
            map.get(userId).post(tweetId);
        }
        
        /** 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> list = new ArrayList();
            if(!map.containsKey(userId)) return list;
            
            Set<Integer> users = map.get(userId).followed;
            PriorityQueue<Tweet> pq = new PriorityQueue<Tweet>((a, b) -> b.time - a.time);
            for(int user: users) {
                Tweet t = map.get(user).tweet_head;//这样能保证至少pq里有一个是最新的tweet
                if(t != null) pq.offer(t);
            }
            int n = 0;
            while(!pq.isEmpty() && n < 10) {
                Tweet t = pq.poll(); //此刻最新的tweet
                list.add(t.id);
                n++;
                if(t.next != null) pq.offer(t.next);//如果这个user还有,就继续压到pq里
               
            }
            return list;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) {
            if(!map.containsKey(followerId)) {
                User user = new User(followerId);
                map.put(followerId, user);
            }
            if(!map.containsKey(followeeId)) {
                User user = new User(followeeId);
                map.put(followeeId, user);
            }
            map.get(followerId).follow(followeeId);
        }
        
        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        public void unfollow(int followerId, int followeeId) {
            if(!map.containsKey(followerId) || followerId == followeeId) return; //不需要检查followeeId是否存在
            map.get(followerId).followed.remove(followeeId);
        }
    }

    https://leetcode.com/problems/design-twitter/discuss/82825/Java-OO-Design-with-most-efficient-function-getNewsFeed

  • 相关阅读:
    HTML超链接标签—链接QQ在线聊天
    超链接标签-QQ邮箱链接经验分享
    数据类型转换的事项和注释
    关键字、标识符、常量、变量的(定义)
    WendosiDOS命令的一些使用命令
    Map集合
    Set集合 HashSet集合 LInkHathSet集合
    增强for循环
    22_迭代器
    包装类
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13408505.html
Copyright © 2011-2022 走看看