[LeetCode] 355. Diseño de Twitter

Tengo que decir que es realmente fuerte

paquete leetcode; 

import java.util. * ; 

/ ** 
 * @author doyinana 
 * @create 2020-04-14 0:29 
 * / 
public  class Twitter {
     public  static  int timestamp = 0 ; 

    Tweet de clase pública  {
         public int tweetId;
        tiempo int público ;
        Tweet público siguiente;
        Tweet público ( int tweetId, int time) {
             this .tweetId = tweetId;
            esta  .time = tiempo;
            this .next = null ; 
        } 
    } 

    usuario de clase pública  {
         private int userId;
        public Set <Integer> seguido;
        Tweet público headTweet; Usuario público ( int userId) { 
            seguido = nuevo HashSet <> ();
            this .userId = userId;
            this .headTweet = null ;
            // 用户 关注 一下 自己 
            seguir ( esto 

        .userId); 
        } 

        public  void follow ( int userId) { 
            followed.add (userId); 
        } 

        public  void unfollow ( int userId) {
             // 不可以 取 关 自己
            if (userId! = this .userId) 
                followed.remove (userId); 
        }

         publicación pública  nula ( int tweetId) { 
            Tweet twt = nuevo Tweet (tweetId, marca de tiempo); 
            marca de tiempo ++ ; 
            twt.next =  headTweet;
            headTweet= twt; 
        } 
    }

     HashMap privado <entero, usuario> userHashMap = nuevo HashMap <> (); 

    public  void postTweet ( int userId, int tweetId) {
         if (! userHashMap.containsKey (userId)) { 
            userHashMap.put (userId, new User (userId)); 
        } 
        userHashMap.get (userId) .post (tweetId); 
    } 

    public List <Integer> getNewsFeed ( int userId) { 
        List <Integer> result = new ArrayList <>Si();
        (! userHashMap.containsKey (userId)) {
             resultado devuelto ; 
        } 
        Establecer <Integer> usuarios = userHashMap.get (userId) .followed; 
        PriorityQueue <Tweet> priorityQueue = new PriorityQueue <> (users.size (), (a, b) -> 
                (b.time - a.time));
        for ( int id: users) { 
            Tweet tweet = userHashMap.get (id) .headTweet;
            if (tweet == null ) continuar ; 
            priorityQueue.add (tweet); 
        } 
        mientras (!priorityQueue.isEmpty ()) {
             if (result.size () == 10 ) {
                 break ; 
            } 
            Tweet tweet = priorityQueue.poll (); 
            result.add (tweet.tweetId); 
            if (tweet.next! = null ) { 
                priorityQueue.add (tweet.next); 
            } 
        } 
        devolver resultado; 
    } 

    public  void follow ( int followerId, int followeeId) {
         if (!  userHashMap.containsKey (followerId)) {
            userHashMap.put (followerId, new User (followerId)); 
        } 
        si (! userHashMap.containsKey (followeeId)) { 
            userHashMap.put (followeeId, nuevo usuario (followeeId)); 
        } 
        userHashMap.get (followerId) .Siga (followeeId); 
    } 

    pública  vacío no seguir ( int followerId, int followeeId) {
         if (userHashMap.containsKey (followerId)) { 
            userHashMap.get (followerId) .unfollow (followeeId); 
        } 
    } 
}

 

Supongo que te gusta

Origin www.cnblogs.com/doyi111/p/12695329.html
Recomendado
Clasificación