lintcode(501)迷你推特

描述:

实现一个迷你的推特,支持下列几种方法
1.postTweet(user_id, tweet_text). 发布一条推特.
2.getTimeline(user_id). 获得给定用户最新发布的十条推特,按照发布时间从最近的到之前排序
3.getNewsFeed(user_id). 获得给定用户的朋友或者他自己发布的最新十条推特,从发布时间最近到之前排序
4.follow(from_user_id, to_user_id). from_user_id 关注 to_user_id.
5.unfollow(from_user_id, to_user_id). from_user_id 取消关注 to_user_id.

样例:

postTweet(1, "LintCode is Good!!!")
>> 1
getNewsFeed(1)
>> [1]
getTimeline(1)
>> [1]
follow(2, 1)
getNewsFeed(2)
>> [1]
unfollow(2, 1)
getNewsFeed(2)
>> []

思路:

创建全局的状态列表list和用户hashmap,所有的用户状态都存储在list中tweet包含user_id,在进行操作之前要记得判空

/**
 * Definition of Tweet:
 * public class Tweet {
 *     public int id;
 *     public int user_id;
 *     public String text;
 *     public static Tweet create(int user_id, String tweet_text) {
 *         // This will create a new tweet object,
 *         // and auto fill id
 *     }
 * }
 */
public class MiniTwitter {
    ArrayList<Tweet> TweetList = new ArrayList<Tweet>();
    HashMap<Integer , HashSet<Integer>> Follows = new HashMap<Integer , HashSet<Integer>>();
    public MiniTwitter() {
        // initialize your data structure here.
    }

    // @param user_id an integer
    // @param tweet a string
    // return a tweet
    public Tweet postTweet(int user_id, String tweet_text) {
        //  Write your code here
        Tweet state = Tweet.create(user_id , tweet_text);
        TweetList.add(state);
        return state;
    }

    // @param user_id an integer
    // return a list of 10 new feeds recently
    // and sort by timeline
    public List<Tweet> getNewsFeed(int user_id) {
        // Write your code here
        List<Tweet> result = new ArrayList<Tweet>();
        int count = 0;
        HashSet<Integer> temp = Follows.get(user_id);
        for(int i = TweetList.size() - 1;i>=0;i--){
            if(TweetList.get(i).user_id == user_id ){
                result.add(TweetList.get(i));
                count++;
            }
            if(temp != null && temp.contains(TweetList.get(i).user_id)){
                result.add(TweetList.get(i));
                count++;
            }
            if(count >= 10){
                break;
            }
        }
        return result;
    }
        
    // @param user_id an integer
    // return a list of 10 new posts recently
    // and sort by timeline
    public List<Tweet>  getTimeline(int user_id) {
        // Write your code here
        List<Tweet> result = new ArrayList<Tweet>();
        int count = 0;
        for(int i = TweetList.size() - 1;i>=0;i--){
            if(TweetList.get(i).user_id == user_id){
                result.add(TweetList.get(i));
                count++;
            }
            if(count >= 10){
                break;
            }
        }
        return result;
    }

    // @param from_user_id an integer
    // @param to_user_id an integer
    // from user_id follows to_user_id
    public void follow(int from_user_id, int to_user_id) {
        // Write your code here
        if(Follows.get(from_user_id) == null){
            Follows.put(from_user_id , new HashSet<Integer>() );
        }
        if(!Follows.get(from_user_id).contains(to_user_id)){
            Follows.get(from_user_id).add(to_user_id);
        }
    }

    // @param from_user_id an integer
    // @param to_user_id an integer
    // from user_id unfollows to_user_id
    public void unfollow(int from_user_id, int to_user_id) {
        // Write your code here
        if(Follows.get(from_user_id) == null){
            Follows.put(from_user_id , new HashSet<Integer>() );
        }
        if(Follows.get(from_user_id).contains(to_user_id)){
            Follows.get(from_user_id).remove(to_user_id);
        }
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值