美文网首页
设计推特

设计推特

作者: HannahLi_9f1c | 来源:发表于2021-01-12 20:46 被阅读0次

有两个用例过不了,好忧伤啊~

   public  static void main(String[] args) {
        //["Twitter","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","postTweet","getNewsFeed","follow","getNewsFeed"]
        //[[],[2,5],[1,3],[1,101],[2,13],[2,10],[1,2],[2,94],[2,505],[1,333],[1,22],[2],[2,1],[2]]
       Twitter twitter = new Twitter();
        twitter.postTweet(2,5);
        twitter.postTweet(1,3);
        twitter.postTweet(1,101);
        twitter.postTweet(2,13);
        twitter.postTweet(2,10);
        twitter.postTweet(1,2);
        twitter.postTweet(2,94);
        twitter.postTweet(2,505);
        twitter.postTweet(1,333);
        twitter.postTweet(1,22);
        twitter.getNewsFeed(2);
        twitter.follow(2,1);
        twitter.getNewsFeed(2);

    }
        private int count = 0;
        class Node{
            private long time;
            private int messageId;
            public Node(long time, int messageId) {
                this.time = time;
                this.messageId = messageId;
            }
        }
        private Map<Integer, Set<Integer>> followeeIds;
        private Map<Integer, List<Node>> news;
        public Twitter() {
            followeeIds = new HashMap<>();
            news = new HashMap<>();
        }

        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            Node n = new Node(count++,tweetId);
            List<Node> myNew = news.getOrDefault(userId,new ArrayList<>());
            myNew.add(n);
            news.put(userId, myNew);
        }

        /** 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<Node> myNew ;
            if(news.get(userId)==null||news.get(userId).isEmpty()){
                myNew =  new ArrayList<>();
            } else {
                myNew = new ArrayList<>(news.get(userId));
            }
            Set<Integer> followees = followeeIds.get(userId);
            if(followees!=null){
                for(Integer f: followees) {
                    List<Node> otherNew = news.get(f);
                    if(otherNew!=null)
                        myNew.addAll(otherNew);
                }
            }
            Collections.sort(myNew, new Comparator<Node>() {
                @Override
                public int compare(Node o1, Node o2) {
                    return o2.time-o1.time>0?1:-1;
                }
            });
            List<Integer> res = new ArrayList<>();
            for(int i = 0; i < 10&&i<myNew.size(); i++) {
                if(!res.contains(myNew.get(i).messageId))
                  res.add(myNew.get(i).messageId);
            }
            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> follewees = followeeIds.getOrDefault(followerId, new HashSet<>());
            follewees.add(followeeId);
            followeeIds.put(followerId, follewees);

        }

        /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
        public void unfollow(int followerId, int followeeId) {
            Set<Integer> follewees = followeeIds.get(followerId);
            follewees.remove(followeeId);
            followeeIds.put(followerId, follewees);
        }

相关文章

  • 设计推特

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/design...

  • 设计推特

    题目: 题目的理解: 保存用户和推特,用户关注的人,进行增删查。 python实现 想看最优解法移步此处 提交 /...

  • 设计推特

    有两个用例过不了,好忧伤啊~

  • LeetCode 355 design-twitter

    LeetCode 355 design-twitter 题目355.设计推特 设计一个简化版的推特(Twitter...

  • 355-设计推特

    设计推特 题目 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关...

  • leetcode 设计推特

    题目描述: https://leetcode-cn.com/problems/design-twitter/ 解 ...

  • 变分自编码器(VAE和LSTM)和聚类方法进行机器人检测-(RT

    摘要 从推特上学习推特的行为模式,收集了1000条推特数据。设计了一个新的方法来区分账号。通过“RTT”技术发现来...

  • Leetcode355. 设计推特

    题目 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括...

  • leetcode-355. 设计推特

    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)...

  • 特推

    限时活动-44一套包邮 1 独角兽唇釉套盒+樱花十色眼影盘 2 爱丽小屋冰欺凌唇釉4支 3 兰蔻摇摇乐+3ce春...

网友评论

      本文标题:设计推特

      本文链接:https://www.haomeiwen.com/subject/wuneaktx.html