调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

Design a system like Facebook with the following features:

  1. A user can write a post.
  2. Two users can become friends with each other.
  3. Users can see all the posts written by their friends.

Implement the Facebook class:

  • Facebook() Initializes the object.
  • void writePost(int userId, String postContent) The user with id userId writes a post with the content postContent.
  • void addFriend(int user1, int user2) user1 and user2 become friends with each other. This call should be ignored if user1 and user2 are already friends.
  • List<String> showPosts(int userId) Returns all the posts made by the friends of the user with id userId ordered by the latest ones first, including ones made before they became friends. Note that the posts made by user userId should not be returned.

 

Example 1:

Input
["Facebook", "addFriend", "writePost", "writePost", "writePost", "writePost", "showPosts", "showPosts", "addFriend", "showPosts", "showPosts", "showPosts"]
[[], [1, 2], [1, "postone"], [2, "posttwo"], [3, "postthree"], [2, "postfour"], [2], [3], [2, 3], [1], [2], [3]]
Output
[null, null, null, null, null, null, ["postone"], [], null, ["postfour", "posttwo"], ["postthree", "postone"], ["postfour", "posttwo"]]

Explanation
Facebook facebook = new Facebook();
facebook.addFriend(1, 2); // Users 1 and 2 become friends.
facebook.writePost(1, "postone"); // "postone" is posted by user 1.
facebook.writePost(2, "posttwo"); // "posttwo" is posted by user 2.
facebook.writePost(3, "postthree"); // "postthree" is posted by user 3.
facebook.writePost(2, "postfour"); // "postfour" is posted by user 2.
facebook.showPosts(2); // return ["postone"]
                       // User 2 has only one friend, which is user 1 who has posted one time so far.
facebook.showPosts(3); // return []
                       // User 3 does not have any friends yet, so we return [].
facebook.addFriend(2, 3); // Users 2 and 3 become friends.
facebook.showPosts(1); // return ["postfour", "posttwo"]
                       // The only friend of user 1 is user 2 who has two posts, so we return them.
facebook.showPosts(2); // return ["postthree", "postone"]
                       // Users 1 and 3 are the friends of user 2.
                       // Both users 1 and 3 have one post each, but user 3 posted last,
                       // so we return user 3's post first.
facebook.showPosts(3); // return ["postfour", "posttwo"]
                       // The only friend of user 3 is user 2 who has two posts.

 

Constraints:

  • 1 <= userId <= 1000
  • 1 <= postContent.length <= 100
  • user1 != user2
  • postContent consists of lowercase English letters.
  • At most 100 calls will be made to writePost.
  • At most 1000 calls will be made to addFriend.
  • At most 100 calls will be made to showPosts.
通过次数
5
提交次数
6
通过率
83.3%

相关标签

相关企业

提示 1
Keep track of the posts and the list of friends of each user.

提示 2
Iterate over the friends of the user and their posts.

相似题目

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
["Facebook","addFriend","writePost","writePost","writePost","writePost","showPosts","showPosts","addFriend","showPosts","showPosts","showPosts"]
[[],[1,2],[1,"postone"],[2,"posttwo"],[3,"postthree"],[2,"postfour"],[2],[3],[2,3],[1],[2],[3]]
Source