Implement a simple twitter. Support the following method:
postTweet(user_id, tweet_text)
. Post a tweet.getTimeline(user_id)
. Get the given user's most recently 10 tweets posted by himself, order by timestamp from most recent to least recent.getNewsFeed(user_id)
. Get the given user's most recently 10 tweets in his news feed (posted by his friends and himself). Order by timestamp from most recent to least recent.follow(from_user_id, to_user_id)
. from_user_id followed to_user_id.unfollow(from_user_id, to_user_id)
. from_user_id unfollowed to to_user_id.
Example:
postTweet(1, "LintCode is Good!!!") >> 1 getNewsFeed(1) >> [1] getTimeline(1) >> [1] follow(2, 1) getNewsFeed(2) >> [1] unfollow(2, 1) getNewsFeed(2) >> []
1 /** 2 * 本代码由九章算法编辑提供。没有版权欢迎转发。 3 * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。 4 * - 现有的面试培训课程包括:九章算法班,系统设计班,九章强化班,Java入门与基础算法班, 5 * - 更多详情请见官方网站:http://www.jiuzhang.com/ 6 */ 7 8 /** 9 * Definition of Tweet: 10 * public class Tweet { 11 * public int id; 12 * public int user_id; 13 * public String text; 14 * public static Tweet create(int user_id, String tweet_text) { 15 * // This will create a new tweet object, 16 * // and auto fill id 17 * } 18 * } 19 */ 20 public class MiniTwitter { 21 class Node { 22 public int order; 23 public Tweet tweet; 24 public Node(int o, Tweet t) { 25 this.order = o; 26 this.tweet = t; 27 } 28 } 29 30 class SortByOrder implements Comparator { 31 public int compare(Object obj1,Object obj2) { 32 Node node1 = (Node) obj1; 33 Node node2 = (Node) obj2; 34 if (node1.order < node2.order) 35 return 1; 36 else 37 return -1; 38 } 39 } 40 41 private Map<Integer, Set<Integer>> friends; 42 private Map<Integer, List<Node>> users_tweets; 43 private int order; 44 45 public List<Node> getLastTen(List<Node> tmp) { 46 int last = 10; 47 if (tmp.size() < 10) 48 last = tmp.size(); 49 return tmp.subList(tmp.size() - last, tmp.size()); 50 } 51 52 public List<Node> getFirstTen(List<Node> tmp) { 53 int last = 10; 54 if (tmp.size() < 10) 55 last = tmp.size(); 56 return tmp.subList(0, last); 57 } 58 59 public MiniTwitter() { 60 // initialize your data structure here. 61 this.friends = new HashMap<Integer, Set<Integer>>(); 62 this.users_tweets = new HashMap<Integer, List<Node>>(); 63 this.order = 0; 64 } 65 66 // @param user_id an integer 67 // @param tweet a string 68 // return a tweet 69 public Tweet postTweet(int user_id, String tweet_text) { 70 // Write your code here 71 Tweet tweet = Tweet.create(user_id, tweet_text); 72 if (!users_tweets.containsKey(user_id)) 73 users_tweets.put(user_id, new ArrayList<Node>()); 74 order += 1; 75 users_tweets.get(user_id).add(new Node(order, tweet)); 76 return tweet; 77 } 78 79 // @param user_id an integer 80 // return a list of 10 new feeds recently 81 // and sort by timeline 82 public List<Tweet> getNewsFeed(int user_id) { 83 // Write your code here 84 List<Node> tmp = new ArrayList<Node>(); 85 if (users_tweets.containsKey(user_id)) 86 tmp.addAll(getLastTen(users_tweets.get(user_id))); 87 88 if (friends.containsKey(user_id)) { 89 for (Integer user : friends.get(user_id)) { 90 if (users_tweets.containsKey(user)) 91 tmp.addAll(getLastTen(users_tweets.get(user))); 92 } 93 } 94 95 Collections.sort(tmp, new SortByOrder()); 96 List<Tweet> rt = new ArrayList<Tweet>(); 97 tmp = getFirstTen(tmp); 98 for (Node node : tmp) { 99 rt.add(node.tweet); 100 } 101 return rt; 102 } 103 104 // @param user_id an integer 105 // return a list of 10 new posts recently 106 // and sort by timeline 107 public List<Tweet> getTimeline(int user_id) { 108 // Write your code here 109 List<Node> tmp = new ArrayList<Node>(); 110 if (users_tweets.containsKey(user_id)) 111 tmp.addAll(getLastTen(users_tweets.get(user_id))); 112 113 Collections.sort(tmp, new SortByOrder()); 114 List<Tweet> rt = new ArrayList<Tweet>(); 115 tmp = getFirstTen(tmp); 116 for (Node node : tmp) 117 rt.add(node.tweet); 118 return rt; 119 } 120 121 // @param from_user_id an integer 122 // @param to_user_id an integer 123 // from user_id follows to_user_id 124 public void follow(int from_user_id, int to_user_id) { 125 // Write your code here 126 if (!friends.containsKey(from_user_id)) 127 friends.put(from_user_id, new HashSet<Integer>()); 128 129 friends.get(from_user_id).add(to_user_id); 130 } 131 132 // @param from_user_id an integer 133 // @param to_user_id an integer 134 // from user_id unfollows to_user_id 135 public void unfollow(int from_user_id, int to_user_id) { 136 // Write your code here 137 if (friends.containsKey(from_user_id)) 138 friends.get(from_user_id).remove(to_user_id); 139 } 140 }