leetcode原题
解题思路
- 使用
{userId: number, follow: [], tweet: [{time: number, tweetId: number}]} 存放每个用户信息
- 要注意的是发送推特的时间可能会相等,所有使用
step 来记录,相当于数据库的自增id
- 最后按照时间倒序获取10条最新的推特
var Twitter = function() {
this.twitterList = []
this.step = 0
};
Twitter.prototype.postTweet = function(userId, tweetId) {
if (!this.hasUser(userId)) {
this.twitterList.push(
{
userId: userId,
follow: [],
tweet: [
{
time: new Date().getTime() + this.step,
tweetId: tweetId
}
]
}
)
} else {
const index = this.twitterList.findIndex(el => el.userId === userId)
let user = this.twitterList[index]
if (user.tweet.findIndex(el => el.tweetId === tweetId) === -1) {
user.tweet.push({
time: new Date().getTime() + this.step,
tweetId: tweetId
})
this.twitterList.splice(index, 1, user)
}
}
this.step += 1
console.log('postTweet', `[${userId}, ${tweetId}]`, JSON.stringify(this.twitterList))
};
Twitter.prototype.getNewsFeed = function(userId) {
if (!this.hasUser(userId)) return []
const my = this.twitterList.find(el => el.userId === userId)
const followList = this.twitterList.filter(el => my.follow.includes(el.userId))
const allList = followList.concat([my])
const tweetList = allList.map(el => el.tweet).reduce((all, curr) => all.concat(curr), [])
tweetList.sort((a, b) => b.time - a.time)
let tweetIdList = tweetList.map(el => el.tweetId)
console.log('getNewsFeed', userId, 'allList', JSON.stringify(allList), 'followList', JSON.stringify(followList), 'tweetList', JSON.stringify(tweetList), 'tweetIdList', tweetIdList)
if (tweetIdList.length < 10) {
return tweetIdList
} else {
return tweetIdList.slice(0, 10)
}
};
Twitter.prototype.follow = function(followerId, followeeId) {
if (!this.hasUser(followerId)) {
this.twitterList.push(
{
userId: followerId,
follow: [followeeId],
tweet: []
}
)
} else {
let index = this.twitterList.findIndex(el => el.userId === followerId)
let user = this.twitterList[index]
const idx = user.follow.findIndex(el => el.followeeId)
if (idx > -1) return
user.follow.push(followeeId)
this.twitterList.splice(index, 1, user)
}
console.log('follow', `[${followerId}, ${followeeId}]`, JSON.stringify(this.twitterList))
};
Twitter.prototype.unfollow = function(followerId, followeeId) {
if (!this.hasUser(followerId)) return
const index = this.twitterList.findIndex(el => el.userId === followerId)
let user = this.twitterList[index]
const idx = user.follow.findIndex(el => el === followeeId)
if (idx === -1) return
user.follow.splice(idx, 1)
this.twitterList.splice(index, 1, user)
console.log('unfollow', `[${followerId}, ${followeeId}]`, this.twitterList)
};
Twitter.prototype.hasUser = function(userId) {
if (this.twitterList.length === 0) {
return false
}
return this.twitterList.findIndex(el => el.userId === userId) !== -1
}