LeetCode—355. Design Twitter

思路:建立2个List存储Twitter信息(userId和twitterId),建立一个map<Integer,Map<Integer,Integer>>存储follow信息(默认必须存储自己的,当然可以在get的时候添加自己的),剩下的就是一些细节问题,按照题目的意思来。


GitHub地址https://github.com/corpsepiges/leetcode

点此进入

Java版本和Python版本,求star。



public class Twitter {
    List<Integer> tweetList;
    List<Integer> userList;
    Map<Integer, Map<Integer,Integer>> followMap;
    /** Initialize your data structure here. */
    public Twitter() {
        tweetList=new ArrayList<Integer>();
        userList=new ArrayList<Integer>();
        followMap=new HashMap<Integer, Map<Integer,Integer>>();
    }
    
    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if (followMap.get(userId)==null) {
            Map<Integer,Integer> map=new HashMap<Integer,Integer>();
            map.put(userId, 1);
            followMap.put(userId, map);
        }
        tweetList.add(tweetId);
        userList.add(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. */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> ans=new ArrayList<Integer>();
        if (followMap.get(userId)==null) {
            return ans;
        }
        int i=tweetList.size()-1;
        Set<Integer> follow=followMap.get(userId).keySet();
        while (i>=0&&ans.size()<10) {
            int id=userList.get(i);
            int tweet=tweetList.get(i);
            if (follow.contains(id)) {
                ans.add(tweet);
            }
            i--;
        }
        return ans;
    }
    
    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if (followMap.get(followerId)==null) {
            Map<Integer, Integer> map=new HashMap<Integer,Integer>();
            map.put(followerId, 1);
            followMap.put(followerId, map);
        }
        followMap.get(followerId).put(followeeId, 1);
    }
    
    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if (followerId!=followeeId&&followMap.get(followerId)!=null&&followMap.get(followerId).get(followeeId)!=null) {
            followMap.get(followerId).remove(followeeId);
        }
    }
}

/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值