leetcode 355. Design Twitter 面向對象設計 優(yōu)先隊列 HashMap

Design Twitter
應用面向對象設計(OO)模擬Twitter的幾個功能晴音,分別是:

  1. 發(fā)推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 follow.
  4. 取關unfollow(followerId, followeeId): Follower unfollows a followee.

跟微博一樣??

首先要考慮的是琳要,這里應該有用戶液斜、推文肃拜,用戶很簡單笤闯,一個整形就可以表示响逢。推文需要有,userId唤崭,tweetId拷恨,timestamp(我們這里并不需要詳細的時間,只要有每條推文的先后順數(shù)就可以了)谢肾,對此我們使用struct tweet
接下來分析功能腕侄,每一個用戶都應該有一個數(shù)組來記錄他的關注對象,另外一個數(shù)組紀錄他的推文芦疏,這里可以選擇建一個structure記錄這些數(shù)據(jù)冕杠,但是在這道題里我們只用到了兩個unordered_maps存放對應數(shù)據(jù),在map中每個整形(userId)都有對應的followees和tweets
有了這樣的數(shù)據(jù)結構呢酸茴,postTweet, follow, unfollow都很好實現(xiàn)了分预,只需要對map進行相應操作即可。
有些難得是getNewsFeed這個函數(shù)薪捍,返回用戶關注的最近十條推文噪舀,包括他自己的魁淳。對于這種有順序的東西很容易想到priority_queue, 下面這個solution用一個priority-queue存儲該用戶關注的每個用戶的推文的begin iterator, end iterator. begin iterator用于從每個follow發(fā)的最新的推文中選出最新的。

class Twitter {
    private:
    struct tweet {
        int userId;
        int tweetId;
        int timesStamp;
        tweet(int x, int y, int z): userId(x), tweetId(y), timesStamp(z){};
    };
    struct mycompare {
        bool operator()(pair<list<tweet>::iterator, list<tweet>::iterator> p1,
                        pair<list<tweet>::iterator, list<tweet>::iterator> p2) {
            return p1.first->timesStamp < p2.first->timesStamp;
        }
    };
    int timeLabel = 0;
    unordered_map<int, unordered_set<int>> followees;
    unordered_map<int, list<tweet>> tweets;
public:
    /** Initialize your data structure here. */
    Twitter() {}
    
    /** Compose a new tweet. */
    void postTweet(int userId, int tweetId) {
        followees[userId].insert(userId);
        tweets[userId].push_front(tweet(userId, tweetId, timeLabel));
        timeLabel++;
    }
    
    /** 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. */
    vector<int> getNewsFeed(int userId) {
        vector<int> res;
        if (followees.find(userId) == followees.end()) return res;
        priority_queue<pair<tweet, tweet>,
                       vector<pair<tweet, tweet>>,
                       mycompare> pq;
        for (auto it = followees[userId].begin(); it != followees[userId].end(); it++)
            if (tweets[*it].begin() != tweets[*it].end())
                pq.push(make_pair(* tweets[*it].begin(), * tweets[*it].end()));
        int index = 0;
        while(!pq.empty() && index < 10) {
            auto tmp = pq.top();
            pq.pop();
            res.push_back(tmp.first.tweetId);
            if (++tmp.first != tmp.second)
                pq.push(tmp);
            index++;
        }
        return res;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    void follow(int followerId, int followeeId) {
        followees[followerId].insert(followerId);
        followees[followerId].insert(followeeId);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    void unfollow(int followerId, int followeeId) {
        if (followees.find(followerId) != followees.end() && followerId != followeeId) {
            followees[followerId].erase(followeeId);
        }
    }

};

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * vector<int> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末与倡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子昆稿,更是在濱河造成了極大的恐慌纺座,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件溉潭,死亡現(xiàn)場離奇詭異净响,居然都是意外死亡,警方通過查閱死者的電腦和手機喳瓣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門馋贤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人畏陕,你說我怎么就攤上這事配乓。” “怎么了惠毁?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵犹芹,是天一觀的道長。 經(jīng)常有香客問我鞠绰,道長腰埂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任蜈膨,我火速辦了婚禮屿笼,結果婚禮上,老公的妹妹穿的比我還像新娘翁巍。我一直安慰自己驴一,他們只是感情好,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布曙咽。 她就那樣靜靜地躺著蛔趴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪例朱。 梳的紋絲不亂的頭發(fā)上孝情,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機與錄音洒嗤,去河邊找鬼箫荡。 笑死,一個胖子當著我的面吹牛渔隶,可吹牛的內容都是我干的羔挡。 我是一名探鬼主播洁奈,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼绞灼!你這毒婦竟也來了利术?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤低矮,失蹤者是張志新(化名)和其女友劉穎印叁,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體军掂,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡轮蜕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蝗锥。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片跃洛。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖终议,靈堂內的尸體忽然破棺而出汇竭,到底是詐尸還是另有隱情,我是刑警寧澤痊剖,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布韩玩,位于F島的核電站,受9級特大地震影響陆馁,放射性物質發(fā)生泄漏找颓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一叮贩、第九天 我趴在偏房一處隱蔽的房頂上張望击狮。 院中可真熱鬧,春花似錦益老、人聲如沸彪蓬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽档冬。三九已至,卻和暖如春桃纯,著一層夾襖步出監(jiān)牢的瞬間酷誓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工态坦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留盐数,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓伞梯,卻偏偏與公主長得像玫氢,于是被迫代替她去往敵國和親帚屉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內容