<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)355:設計推特

          共 9821字,需瀏覽 20分鐘

           ·

          2021-08-19 23:58

          算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做 設計推特,我們先來看題面:
          https://leetcode-cn.com/problems/design-twitter/

          設計一個簡化版的推特(Twitter),可以讓用戶實現發(fā)送推文,關注/取消關注其他用戶,能夠看見關注人(包括自己)的最近十條推文。你的設計需要支持以下的幾個功能:
          • postTweet(userId, tweetId): 創(chuàng)建一條新的推文

          • getNewsFeed(userId): 檢索最近的十條推文。每個推文都必須是由此用戶關注的人或者是用戶自己發(fā)出的。推文必須按照時間順序由最近的開始排序。

          • follow(followerId, followeeId): 關注一個用戶

          • unfollow(followerId, followeeId): 取消關注一個用戶


          示例


          Twitter twitter = new Twitter();

          // 用戶1發(fā)送了一條新推文 (用戶id = 1, 推文id = 5).
          twitter.postTweet(1, 5);

          // 用戶1的獲取推文應當返回一個列表,其中包含一個id為5的推文.
          twitter.getNewsFeed(1);

          // 用戶1關注了用戶2.
          twitter.follow(1, 2);

          // 用戶2發(fā)送了一個新推文 (推文id = 6).
          twitter.postTweet(2, 6);

          // 用戶1的獲取推文應當返回一個列表,其中包含兩個推文,id分別為 -> [6, 5].
          // 推文id6應當在推文id5之前,因為它是在5之后發(fā)送的.
          twitter.getNewsFeed(1);

          // 用戶1取消關注了用戶2.
          twitter.unfollow(1, 2);

          // 用戶1的獲取推文應當返回一個列表,其中包含一個id為5的推文.
          // 因為用戶1已經不再關注用戶2.
          twitter.getNewsFeed(1);


          解題

          https://my.oschina.net/jiansin/blog/4835060

          • 發(fā)布動態(tài)的時候,需要先判斷用戶是否存在,如果存在,新增發(fā)布信息,如果不存在,需要新建立用戶發(fā)布信息。

          • 用戶關注其他用戶的時候,如果用戶不存在,需要創(chuàng)建用戶并添加關注用戶。

          • 用戶取消關注的時候,需要判斷用戶是否存在,如果存在,則刪除關注用戶,否則不操作。

          • 獲取用戶最新10條動態(tài)的時候,需要采用優(yōu)先隊列輔助解決,當用戶存在的時候,需要獲取用戶的關注對象和用戶自身發(fā)布的所有推文信息,借助優(yōu)先隊列獲取最新發(fā)布的10條信息。


          public class Twitter {
             //代表發(fā)布的先后順序
              private static int pubNum = 0;

              private class TwitterNews {


                  private int tweetId;

                  private int pubTime;

                  public TwitterNews(int tweetId) {
                      pubNum = pubNum + 1;
                      this.tweetId = tweetId;
                      this.pubTime = pubNum;
                  }
              }

              private class TwitterUser {

                  private int userId;

                  /**
                   * 用戶的關注對象
                   */

                  private HashSet<Integer> userFollows;

                  /**
                   * 用戶發(fā)布的信息對象
                   */

                  private HashSet<TwitterNews> userPubNews;


                  TwitterUser(int userId) {
                      this.userId = userId;

                      this.userFollows = new HashSet<>();

                      this.userPubNews = new LinkedHashSet<TwitterNews>();
                  }
              }


              private HashMap<Integer, TwitterUser> IDToUser;

              public Twitter() {

                  IDToUser = new HashMap<Integer, TwitterUser>();
              }

              /**
               * 發(fā)布一條動態(tài)
               * @param userId
               * @param tweetId
               */

              public void postTweet(int userId, int tweetId) {

                  TwitterNews twitterNews = new TwitterNews(tweetId);

                  if (IDToUser.containsKey(userId)) {
                      TwitterUser tweetUser = IDToUser.get(userId);
                      tweetUser.userPubNews.add(twitterNews);
                  } else {
                      TwitterUser tweetUser = new TwitterUser(userId);
                      tweetUser.userPubNews.add(twitterNews);
                      IDToUser.put(userId, tweetUser);
                  }
              }

              /**
               * 獲取最新的動態(tài)發(fā)布信息
               * @param userId
               * @return
               */

              public List<Integer> getNewsFeed(int userId) {

                  if (!IDToUser.containsKey(userId)) {
                      return new ArrayList<>();
                  }
                  PriorityQueue<TwitterNews> queue = new PriorityQueue<TwitterNews>(((o1, o2) -> o2.pubTime - o1.pubTime));

                  HashSet<Integer> userFollows = IDToUser.get(userId).userFollows;

                  userFollows.add(userId);

                  for (int userID : userFollows) {

                      TwitterUser twitterUser = IDToUser.get(userID);

                      /**
                       * 沒有用戶則繼續(xù)尋找下一個用戶
                       */

                      if (twitterUser == null) {
                          continue;
                      }
                      HashSet<TwitterNews> userPubNews = twitterUser.userPubNews;

                      for (TwitterNews userPubNew : userPubNews) {
                          queue.add(userPubNew);
                      }
                  }
                  List<Integer> res = new ArrayList<>();

                  while (!queue.isEmpty()) {
                      if (res.size() == 10) {
                          break;
                      }
                      TwitterNews twitterNews = queue.poll();
                      res.add(twitterNews.tweetId);
                  }
                  return res;
              }

              /**
               * 添加關注人
               * @param followerId
               * @param followeeId
               */

              public void follow(int followerId, int followeeId) {
                  if (IDToUser.containsKey(followerId)) {
                      TwitterUser tweetUser = IDToUser.get(followerId);
                      tweetUser.userFollows.add(followeeId);
                  } else {
                      TwitterUser tweetUser = new TwitterUser(followerId);
                      tweetUser.userFollows.add(followeeId);
                      IDToUser.put(followerId, tweetUser);
                  }
              }

              /**
               * 取消關注人
               * @param followerId
               * @param followeeId
               */

              public void unfollow(int followerId, int followeeId) {
                  if (IDToUser.containsKey(followerId)) {
                      TwitterUser tweetUser = IDToUser.get(followerId);
                      tweetUser.userFollows.remove(followeeId);
                  }
              }

          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力 。

          上期推文:
          LeetCode1-340題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)341:扁平化嵌套列表迭代器
          LeetCode刷題實戰(zhàn)342:4的冪
          LeetCode刷題實戰(zhàn)343:整數拆分
          LeetCode刷題實戰(zhàn)344:反轉字符串
          LeetCode刷題實戰(zhàn)345:反轉字符串中的元音字母
          LeetCode刷題實戰(zhàn)346:數據流中的移動平均值
          LeetCode刷題實戰(zhàn)347:前 K 個高頻元素
          LeetCode刷題實戰(zhàn)348:設計井字棋
          LeetCode刷題實戰(zhàn)349:兩個數組的交集
          LeetCode刷題實戰(zhàn)350:兩個數組的交集 II
          LeetCode刷題實戰(zhàn)351:安卓系統(tǒng)手勢解鎖
          LeetCode刷題實戰(zhàn)352:將數據流變?yōu)槎鄠€不相交區(qū)間
          LeetCode刷題實戰(zhàn)353:貪吃蛇
          LeetCode刷題實戰(zhàn)354:俄羅斯套娃信封問題

          瀏覽 15
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  操笔视频国产 | 国产成人综合久久久久久 | 婷婷综合网 | av无码导航 | 日韩精品人妻无码 |