成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode355. Design Twitter

superPershing / 1115人閱讀

摘要:思路和代碼這道題目本質(zhì)上是考察是否能將數(shù)據(jù)結(jié)構(gòu)的知識(shí)靈活的運(yùn)用于現(xiàn)實(shí)生活中。這題等價(jià)于我們每次都會(huì)比較當(dāng)前所有被關(guān)注者發(fā)布的最新未讀,選出結(jié)果后將其插入結(jié)果集。

題目要求
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:

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

設(shè)計(jì)一個(gè)迷你推特,要求能夠支持以下幾個(gè)方法:發(fā)布推特,關(guān)注用戶,取關(guān)用戶,查看最近的十條關(guān)注用戶發(fā)送的推特。

思路和代碼

這道題目本質(zhì)上是考察是否能將數(shù)據(jù)結(jié)構(gòu)的知識(shí)靈活的運(yùn)用于現(xiàn)實(shí)生活中。從最直觀的想法來看,我們會(huì)有一個(gè)用戶實(shí)體,每個(gè)用戶會(huì)記錄自己關(guān)注的用戶的id,以及記錄自己發(fā)表的所有tweet。這里唯一的難點(diǎn)在于我們?nèi)绾伟凑諘r(shí)間順序獲取tweet流。

這么一想,這題其實(shí)就轉(zhuǎn)換為如何將N個(gè)有序排列的數(shù)組匯合成一個(gè)有序的數(shù)組。這題等價(jià)于我們每次都會(huì)比較當(dāng)前所有被關(guān)注者發(fā)布的最新未讀tweet,選出結(jié)果后將其插入結(jié)果集。這里我們可以利用等價(jià)隊(duì)列幫助我們更快的完成選擇的過程。

public class Twitter {

    
    public Twitter() {
        users = new HashMap<>();
    }
    
    public static int timestamp = 0;
    private Map users;
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(!users.containsKey(userId)) {
            User user = new User(userId);
            users.put(userId, user);
        }
        
        User user = users.get(userId);
        user.tweet(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 getNewsFeed(int userId) {
        List result = new ArrayList();
        if(!users.containsKey(userId)) {
            return result;
        }
        User user = users.get(userId);
        
        PriorityQueue queue = new PriorityQueue<>(user.followed.size());
        for(int followee : user.followed) {
            User tmp = users.get(followee);
            if(tmp != null && tmp.headTweet != null) {
                queue.offer(tmp.headTweet);
            } 
        }
        
        while(!queue.isEmpty() && result.size() < 10) {
            Tweet t = queue.poll();
            result.add(t.tweetId);
            if(t.next != null) {
                queue.offer(t.next);
            }
        }
        return result;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if(!users.containsKey(followerId)) {
            User user = new User(followerId);
            users.put(followerId, user);
        }
        if(!users.containsKey(followeeId)) {
            User user = new User(followeeId);
            users.put(followeeId, user);
        }
        
        User user = users.get(followerId);
        user.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(!users.containsKey(followerId) || followerId == followeeId) {
            return;
        }
      
        
        User user = users.get(followerId);
        user.unfollow(followeeId);
    }
    
    public static class User{
        
        int userId;
        
        Set followed;
        
        Tweet headTweet;
        
        public User(int userId) {
            this.userId = userId;
            this.followed = new HashSet<>();
            follow(userId);
        }
        
        public void follow(int userId) {
            followed.add(userId);
        }
        
        public void unfollow(int userId) {
            followed.remove(userId);
        }
        
        public void tweet(int tweetId) {
            Tweet tweet = new Tweet(tweetId);
            tweet.next = headTweet;
            headTweet = tweet;
        }
        
    }
    
    public static class Tweet implements Comparable{
        
        int tweetId;
        
        Tweet next;
        
        int time;
        
        public Tweet(int tweetId) {
            this.tweetId = tweetId;
            this.time = timestamp++;
        }

        @Override
        public int compareTo(Tweet o) {
            return o.time - this.time;
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/73577.html

相關(guān)文章

  • 355. Design Twitter , 用23. Merge k Sorted Lists和OO

    摘要:的想法就是用每次得到最小的鏈表頭的值,輸出。每個(gè)都有一個(gè)關(guān)注人列表,用儲(chǔ)存。用戶可以發(fā)消息,關(guān)注別人,取消關(guān)注別人??梢韵到y(tǒng)得到關(guān)注人的消息合集,這個(gè)方法必須在這個(gè)層級(jí)。因?yàn)橛脩糁恢雷约旱男畔ⅰ? Merge k Sorted Lists /** * Definition for singly-linked list. * public class ListNode { * ...

    1fe1se 評(píng)論0 收藏0
  • [LeetCode/LintCode] Design Twitter/Mini Twitter

    摘要:首先建立按時(shí)間戳從大到小排列的,找到中的,出其中在中存在的,把每一個(gè)的推特鏈表放入,再?gòu)闹腥☆^十條推特的放入結(jié)果數(shù)組。 Design Twitter Note 建立兩個(gè)HashMap,一個(gè)存user,一個(gè)存tweets。以及整型的時(shí)間戳timestamp。user的k-v pair是userId-follower_set,tweets的k-v pair是userId-tweets_li...

    honmaple 評(píng)論0 收藏0
  • leetcode 341 Flatten Nested List Iterator 以及其他Iter

    摘要:返回的是表示是否走到了結(jié)尾。起到的就是緩存作用,因?yàn)檎{(diào)用之后馬上就走到下一個(gè)了。如果調(diào)用,返回用得到和最初的輸入相同的做相同的步驟存入不斷拆開得到結(jié)果。思想就是來自括號(hào),后面也會(huì)跟進(jìn)的專題 Iterator其實(shí)就是一個(gè)單鏈表,無法回頭看。java里很多數(shù)據(jù)結(jié)構(gòu)都有這個(gè)接口,使用時(shí)需要initalize,得到一個(gè)iterator. 調(diào)用next()返回的是一個(gè)object, 指向的是下一...

    chaosx110 評(píng)論0 收藏0
  • Leetcode PHP題解--D87 705. Design HashSet

    摘要:題目鏈接題目分析設(shè)計(jì)一個(gè)哈希類。需要有添加元素函數(shù),判斷元素存在的函數(shù),移除元素函數(shù)。思路這真的沒什么好說的了我把要存的值作為數(shù)組的鍵存儲(chǔ)。最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 D87 705. Design HashSet 題目鏈接 705. Design HashSet 題目分析 設(shè)計(jì)一個(gè)哈希類。 需要有add添加元素函數(shù),contains判斷元素存在的函數(shù),remov...

    why_rookie 評(píng)論0 收藏0
  • Leetcode PHP題解--D75 706. Design HashMap

    摘要:題目鏈接題目分析自行設(shè)計(jì)一個(gè)。需要實(shí)現(xiàn)題目?jī)?nèi)指定的函數(shù)。思路我覺得這個(gè)沒什么好說的吧最終代碼若覺得本文章對(duì)你有用,歡迎用愛發(fā)電資助。 D75 706. Design HashMap 題目鏈接 706. Design HashMap 題目分析 自行設(shè)計(jì)一個(gè)hashmap。 需要實(shí)現(xiàn)題目?jī)?nèi)指定的函數(shù)。 思路 我覺得這個(gè)沒什么好說的吧… 最終代碼

    sf190404 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<