• 【Leetcode】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.

    题目分析:

        这是一道实现具体类的题目,一般来说这样的题只要理解每个方法的具体用法就不难做出。在这道题目中,需要一个发twitter的方法,需要一个加关注和一个取消关注的方法,以及一个浏览twitter的方法。所以我们需要在类中有一个保存userId和他发送的所有twitter的关系的Map,以及userId和他所关注的其他userid的关系的Map。浏览twitter时只要找到用户自己发的twitter,和通过自己所关注的人所发的twitter,并从中选取出10条最近发送的即可。

        这道题我在实现的时候花了比较长的时间,主要有两点没有注意到:

    1.list的addAll()方法不能参数不能为null,否则会报错。

    2.twitterId并不代表发送顺序,需要自己设置一个表示发送顺序的时间戳。

       注意到以上两点,这个题就不难解决。

    具体代码:

      1 public class Twitter {
      2     private static int order=0;
      3     private Map<Integer,Set<Message>> messages;
      4     private Map<Integer,Set<Integer>> followers;
      5     /** Initialize your data structure here. */
      6     public Twitter() {
      7         messages = new HashMap<Integer,Set<Message>>();
      8         followers = new HashMap<Integer,Set<Integer>>();
      9     }
     10    
     11     /** Compose a new tweet. */
     12     public void postTweet(int userId, int tweetId) {
     13         Message m = new Message(userId,tweetId,order++);
     14         Set<Message> set=messages.getOrDefault(userId, new HashSet<Message>());
     15         set.add(m);
     16         messages.put(userId, set);
     17     }
     18     
     19     /** 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. */
     20     public List<Integer> getNewsFeed(int userId) {
     21         List<Message> sets = new ArrayList<Message>();
     22         //userId发布的消息
     23         Set<Message> set = messages.getOrDefault(userId, new HashSet<Message>());
     24         sets.addAll(set);
     25         //找出他所关注的人发布的消息
     26         Set<Integer> follow = followers.get(userId);
     27         if(follow!=null){
     28             for(int i:follow){
     29                 set=messages.getOrDefault(i, new HashSet<Message>());
     30                 sets.addAll(set);
     31             }
     32         }
     33         //对找出的消息进行排序,并找出最近翻出的10条返回
     34         List<Integer> result=new ArrayList<Integer>();
     35         Compare c =new Compare();
     36         sets.sort(c);
     37         for(int i=0;i<sets.size()&&i<10;i++){
     38             Message m=sets.get(i);
     39             result.add(m.twitterId);
     40         }
     41         return result;
     42     }
     43     
     44     /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
     45     public void follow(int followerId, int followeeId) {
     46         if(followeeId==followerId)
     47             return;
     48         Set<Integer> set = followers.getOrDefault(followerId, new HashSet<Integer>());
     49         set.add(followeeId);
     50         followers.put(followerId, set);
     51         
     52     }
     53     
     54     /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
     55     public void unfollow(int followerId, int followeeId) {
     56         if(followeeId==followerId)
     57             return;
     58         if(!followers.containsKey(followerId)){
     59              ;
     60          }
     61          else{
     62              Set<Integer> set = followers.get(followerId);
     63              set.remove(followeeId);
     64              //如果userid的用户已经没有关注的人,讲他的记录从map中删除掉
     65              if(set.size()==0){
     66                  followers.remove(followerId);
     67              }
     68              else{
     69              followers.put(followerId, set);
     70              }
     71          }
     72     }
     73 }
     74 //封装一条twitter的类
     75 class Message{
     76     int userId;
     77     int twitterId;
     78     int order;
     79     public Message(int userId, int twitterId,int order) {
     80         super();
     81         this.userId = userId;
     82         this.twitterId = twitterId;
     83         this.order=order;
     84     }
     85 }
     86 //对消息进行排序的比较器
     87 class Compare implements Comparator<Message>{
     88 
     89     @Override
     90     public int compare(Message m1, Message m2) {
     91         // TODO Auto-generated method stub
     92         if(m1.order>m2.order)
     93             return -1;
     94         else if(m1.order<m2.order)
     95             return 1;
     96         else
     97             return 0;
     98     }
     99     
    100 }
  • 相关阅读:
    常见面试题
    3*0.1 == 0.3 将会返回什么?true 还是 false?
    poj_2186 强连通分支
    强连通分量、割点、桥
    最小生成树
    poj_2349 Kruskal 最小生成树
    poj_1258 prim最小生成树
    最短路径
    poj_1125 Floyd最短路
    poj_1860 SPFA
  • 原文地址:https://www.cnblogs.com/godlei/p/5581931.html
Copyright © 2020-2023  润新知