刷题73——设计推特

112.设计推特

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-twitter

题目描述

设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:

postTweet(userId, tweetId): 创建一条新的推文
getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。
follow(followerId, followeeId): 关注一个用户
unfollow(followerId, followeeId): 取消关注一个用户
示例:

Twitter twitter = new Twitter();

// 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
twitter.postTweet(1, 5);

// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
twitter.getNewsFeed(1);

// 用户1关注了用户2.
twitter.follow(1, 2);

// 用户2发送了一个新推文 (推文id = 6).
twitter.postTweet(2, 6);

// 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
// 推文id6应当在推文id5之前,因为它是在5之后发送的.
twitter.getNewsFeed(1);

// 用户1取消关注了用户2.
twitter.unfollow(1, 2);

// 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
// 因为用户1已经不再关注用户2.
twitter.getNewsFeed(1);

重难点

设计数据结构

题目分析

  1. 定义推特文章的序号、关注的人、推特内容;
  2. 创建新的推文:存放在obj对象中;
  3. 检索最近的十条推文:先循环得出 所有关注的人(包括自己),再对每个人的推题末尾10条数据追加推特序号的数组,对数组按num排序,输出前10;
  4. 关注一个用户:判断全体用户中是否存在这个人,如果存在:判断是否已经关注此人,关注了就跳过,没关注就追加;如果不存在,就直接跳过;
  5. 取消关注一个用户:判断全体用户中是否存在这个人,如果存在:判断是否已经关注此人,关注了就删除,没关注就跳过;如果不存在,就直接跳过。
/**
 * Initialize your data structure here.
 */
var Twitter = function() {
    this.num = [];  //推特序号
    this.focus = []; //关注的人
    this.news = [];  //推特内容
};

/**
 * Compose a new tweet. 
 * @param {number} userId 
 * @param {number} tweetId
 * @return {void}
 */
Twitter.prototype.postTweet = function(userId, tweetId) {
    var obj = {"tweetId":tweetId,num:++this.num};
    this.news[userId] ? this.news[userId].push(obj) : this.news[userId] = [obj];
};

/**
 * 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. 
 * @param {number} userId
 * @return {number[]}
 */
Twitter.prototype.getNewsFeed = function(userId) {
    var arr = this.focus[userId] ? this.focus[userId] : [];
    if(arr.indexOf(userId) == -1){
        arr.push(userId);
    }
    var all = [];
    for(var i=0; i<arr.length; i++){
        if(!this.news[arr[i]])
            continue;
        if(this.news[arr[i]].length -10 >= 0){
            all = all.concat(this.news[arr[i]].slice(this.news[arr[i]].length-10, this.news[arr[i]].length));
        }else{
            all = all.concat(this.news[arr[i]].slice(0, this.news[arr[i]].length));
        }
    }
    //按推特序号排序
    all.sort(function(a,b){
        return  b.num - a.num;
    })
    var res = [];
    for(var i=0; i<(all.length > 10?10:all.length); i++){
        res.push(all[i]["tweetId"]);
    }
    return res;
};

/**
 * Follower follows a followee. If the operation is invalid, it should be a no-op. 
 * @param {number} followerId 
 * @param {number} followeeId
 * @return {void}
 */
Twitter.prototype.follow = function(followerId, followeeId) {
    if(this.focus[followerId]){
        if(this.focus[followerId].indexOf(followeeId) == -1){
            this.focus[followerId].push(followeeId);
        }
        else
            return;
    }else{
        this.focus[followerId] = [followeeId];
    }
};

/**
 * Follower unfollows a followee. If the operation is invalid, it should be a no-op. 
 * @param {number} followerId 
 * @param {number} followeeId
 * @return {void}
 */
Twitter.prototype.unfollow = function(followerId, followeeId) {
    if(!this.focus[followerId]) return;
    else{
        if(this.focus[followerId].indexOf(followeeId) != -1){
            this.focus[followerId].splice(this.focus[followerId].indexOf(followeeId),1);
        }
    }
    return;
};

/**
 * Your Twitter object will be instantiated and called as such:
 * var obj = new Twitter()
 * obj.postTweet(userId,tweetId)
 * var param_2 = obj.getNewsFeed(userId)
 * obj.follow(followerId,followeeId)
 * obj.unfollow(followerId,followeeId)
 */

  

猜你喜欢

转载自www.cnblogs.com/liu-xin1995/p/12702707.html