Skip to content

Commit 67ead73

Browse files
refactor 355
1 parent 1b727d3 commit 67ead73

File tree

1 file changed

+20
-44
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-44
lines changed

src/main/java/com/fishercoder/solutions/_355.java

+20-44
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,6 @@
88
import java.util.PriorityQueue;
99
import java.util.Set;
1010

11-
/**
12-
* 355. Design Twitter
13-
*
14-
* Design a simplified version of Twitter where users can post tweets,
15-
* 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:
16-
postTweet(userId, tweetId): Compose a new tweet.
17-
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.
18-
follow(followerId, followeeId): Follower follows a followee.
19-
unfollow(followerId, followeeId): Follower unfollows a followee.
20-
21-
Example:
22-
23-
Twitter twitter = new Twitter();
24-
25-
// User 1 posts a new tweet (userId = 5).
26-
twitter.postTweet(1, 5);
27-
28-
// User 1's news feed should return a list with 1 tweet userId -> [5].
29-
twitter.getNewsFeed(1);
30-
31-
// User 1 follows user 2.
32-
twitter.follow(1, 2);
33-
34-
// User 2 posts a new tweet (userId = 6).
35-
twitter.postTweet(2, 6);
36-
37-
// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
38-
// Tweet userId 6 should precede tweet userId 5 because it is posted after tweet userId 5.
39-
twitter.getNewsFeed(1);
40-
41-
// User 1 unfollows user 2.
42-
twitter.unfollow(1, 2);
43-
44-
// User 1's news feed should return a list with 1 tweet userId -> [5],
45-
// since user 1 is no longer following user 2.
46-
twitter.getNewsFeed(1);
47-
*/
4811
public class _355 {
4912

5013
public static class Solution1 {
@@ -60,9 +23,12 @@ class Tweet {
6023
public int time;
6124
public int id;
6225
public Tweet next;
63-
/**have a pointer,
26+
27+
/**
28+
* have a pointer,
6429
* so we could be more memory efficient when retrieving tweets,
65-
* think about merging k sorted lists*/
30+
* think about merging k sorted lists
31+
*/
6632

6733
public Tweet(int id) {
6834
this.id = id;
@@ -237,13 +203,17 @@ public Tweet(int id) {
237203
}
238204
}
239205

240-
/** Initialize your data structure here. */
206+
/**
207+
* Initialize your data structure here.
208+
*/
241209
public Twitter() {
242210
map = new HashMap<>();
243211
timestamp = 0;
244212
}
245213

246-
/** Compose a new tweet. */
214+
/**
215+
* Compose a new tweet.
216+
*/
247217
public void postTweet(int userId, int tweetId) {
248218
if (!map.containsKey(userId)) {
249219
User user = new User(userId);
@@ -252,7 +222,9 @@ public void postTweet(int userId, int tweetId) {
252222
map.get(userId).postTweet(tweetId);
253223
}
254224

255-
/** 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. */
225+
/**
226+
* 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.
227+
*/
256228
public List<Integer> getNewsFeed(int userId) {
257229
List<Integer> result = new LinkedList<>();
258230
if (!map.containsKey(userId)) {
@@ -282,7 +254,9 @@ public List<Integer> getNewsFeed(int userId) {
282254
return result;
283255
}
284256

285-
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
257+
/**
258+
* Follower follows a followee. If the operation is invalid, it should be a no-op.
259+
*/
286260
public void follow(int followerId, int followeeId) {
287261
if (!map.containsKey(followerId)) {
288262
map.put(followerId, new User(followerId));
@@ -293,7 +267,9 @@ public void follow(int followerId, int followeeId) {
293267
map.get(followerId).follow(followeeId);
294268
}
295269

296-
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
270+
/**
271+
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
272+
*/
297273
public void unfollow(int followerId, int followeeId) {
298274
if (!map.containsKey(followerId) || followeeId == followerId) {
299275
return;

0 commit comments

Comments
 (0)