LeetCode355。デザインTwitter



Leetcode 355 Design Twitter



package LeetCode import java.util.* /** * Your Twitter object will be instantiated and called as such: * Twitter obj = new Twitter() * obj.postTweet(userId,tweetId) * List param_2 = obj.getNewsFeed(userId) * obj.follow(followerId,followeeId) * obj.unfollow(followerId,followeeId) */ public class L355_DesignTwitter { int feedNum private int order=0 Map follows//Following people Map tweets//user's tweet collection Private class Tweet{//record tweet Integer tweetID //Long timestamp Integer timestamp public Tweet(int id){ tweetID=id //timestamp=System.currentTimeMillis() timestamp=order++ } /* HashSet can't add duplicate elements, when calling the add(Object) method, First, the object's hashCode method is called to determine whether the hashCode already exists. If it does not exist, the element is directly inserted. If it already exists, call the equals method of the Object object to determine whether to return true. If true, the element already exists. If it is false, insert the element. */ public int hashCode() { return tweetID.hashCode() } @Override public boolean equals(Object obj) { if (this == obj) return true if (obj == null) return false Tweet tweet = (Tweet) obj return tweet.tweetID.equals(this.tweetID) } } /** Initialize your data structure here. */ Public L355_DesignTwitter() {//Not created for a specific user, this does not contain the user id.//You need to modify this and the class name when submitting feedNum=10 follows=new HashMap () tweets=new HashMap () } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { if(!tweets.containsKey(userId)) { LinkedList ll=new LinkedList() tweets.put(userId,ll) } Tweet tweet=new Tweet(tweetId) tweets.get(userId).add(tweet) } private static class TweetComparator implements Comparator{ @Override public int compare(Tweet o1, Tweet o2) { /*if (o1.timestamp.compareTo(o2.timestamp) == 0) { return -o1.tweetID.compareTo(o2.tweetID) } else { return o1.timestamp.compareTo(o2.timestamp) }*/ return o1.timestamp.compareTo(o2.timestamp) } } private void sortTweet(List ll){ Collections.sort(ll,new TweetComparator()) } private List removeRepeatTweet(List ll){ Set st=new HashSet(ll) return new LinkedList(st) } /** 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. */ public List getNewsFeed(int userId) { List ll=new LinkedList() if(tweets.containsKey(userId)) { LinkedList temp=tweets.get(userId) if(temp.size()>feedNum) ll.addAll(temp.subList(temp.size()-feedNum,temp.size()))//sublist includes the latter parameter as the content corresponding to the subscript else ll.addAll(temp) } if(follows.containsKey(userId)) { Iterator it=follows.get(userId).iterator() While(it.hasNext()){//gets the user’s attention int followee=it.next() if(tweets.containsKey(followee)) { List temp=tweets.get(followee) if(temp.size()>feedNum) ll.addAll(temp.subList(temp.size()-feedNum,temp.size())) else ll.addAll(temp) } } } Ll=removeRepeatTweet(ll)//De-duplicate. Using hashset will affect the order sortTweet(ll) Int oldIndex / / the number of the earliest tweets returned, if(ll.size()=oldIndexi--)//You can use Lists.reverse(list) to implement list flipping. Here ll list is different from the object of result result.add(ll.get(i).tweetID) return result } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { if(!follows.containsKey(followerId)) { HashSet hs=new HashSet() follows.put(followerId,hs) } follows.get(followerId).add(followeeId) } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { if(!follows.containsKey(followerId)) { return } follows.get(followerId).remove(followeeId) } public static void main(String[] args){ L355_DesignTwitter twitter = new L355_DesignTwitter() /*twitter.postTweet(1, 1) twitter.getNewsFeed(1) twitter.follow(2, 1) twitter.getNewsFeed(2) twitter.unfollow(2, 1) twitter.getNewsFeed(2)*/ /* twitter.postTweet(1,5) System.out.println(twitter.getNewsFeed(1)) twitter.follow(1,2) twitter.postTweet(2,6) System.out.println(twitter.getNewsFeed(1)) twitter.unfollow(1,2) System.out.println(twitter.getNewsFeed(1))*/ /* twitter.postTweet(1,5) twitter.follow(1,1) System.out.println(twitter.getNewsFeed(1))*/ twitter.postTweet(1,5) twitter.postTweet(1,3) twitter.postTweet(1,101) twitter.postTweet(1,13) twitter.postTweet(1,10) twitter.postTweet(1,2) twitter.postTweet(1,94) twitter.postTweet(1,505) twitter.postTweet(1,333) twitter.postTweet(1,22) twitter.postTweet(1,11) System.out.println(twitter.getNewsFeed(1)) } }