leetcode355

该博客介绍了一个Twitter类的实现,其中包括用户类和推文类。用户类维护关注对象列表和自己的推文,推文类包含时间戳。主类使用固定大小的用户数组,用空间换效率,并通过Collections.sort进行排序来生成NewsFeed,展示关注对象和自己的最新推文。关注和取消关注功能也得到了实现。
摘要由CSDN通过智能技术生成

# 解题思路

两个内部类 用户和推文 用户有两个序表,一个存关注对象id,一个存自己发的推特id 推文两个属性,一个是id,一个是利用一个全局变量time生成的t,用来判断顺序 而主类中只有一个属性:用户数组,本来是链表的,但苦于无法将id与用户对应,又觉得链表索引id太慢了,就干脆用数组 先申请一个较大的空间,典型用空间换效率,有取巧的嫌疑,但是确实没办法 其他要注意的就是生成NewsFeed的时候,利用Collections.sort重载依据推文的t来排序,而待排序列由用户的所有关注对象和自己组成

# 代码

public class Twitter {
    users[] userlist = new users[1000];
    int time = 0;
    public class twit {
        int twittid;
        int t;
        int gettime(){
            return t;
        }
        twit(int id) {
            twittid = id;
            time++;
            t=time;
        }
    }
    public class users {
        List<Integer> followees = new ArrayList<>();
        List<twit> twitts = new ArrayList<>();
        public void setTwitts(Integer twitt) {
            this.twitts.add(new twit(twitt));
        }
        public List<Integer> getTwitts() {
            List<twit> copy1=new ArrayList<>();
            copy1.addAll(twitts);
            for (Integer f : followees) {
                if(userlist[f]!=null)
                copy1.addAll(userlist[f].twitts);
            }
            Collections.sort(copy1, new Comparator<twit>() {
                public int compare(twit o1, twit o2) {
                    return o2.gettime() - o1.gettime();
                }
            });
            List<Integer> temp = new ArrayList<>();
            for (twit i : copy1
            ) {
                temp.add(i.twittid);
            }
            return temp.size() < 10 ? temp : temp.subList(0, 10);
        }
    }

    public Twitter() {

    }

    public void postTweet(int userId, int tweetId) {
        if (userlist[userId] == null) {
            users newuser = new users();
            newuser.setTwitts(tweetId);
            userlist[userId] = newuser;
        } else userlist[userId].setTwitts(tweetId);
    }

    public List<Integer> getNewsFeed(int userId) {
        if (userlist[userId] == null) {
            userlist[userId] = new users();
        }
        return userlist[userId].getTwitts();
    }

    public void follow(int followerId, int followeeId) {
        if (userlist[followerId] == null) {
            userlist[followerId] = new users();
        }
        if(followeeId==followerId)
            return;
        if (userlist[followerId].followees.contains(followeeId))
            return;
        else userlist[followerId].followees.add(followeeId);
    }

    public void unfollow(int followerId, int followeeId) {
        if (userlist[followerId] == null) {
            userlist[followerId] = new users();
        }
        if(followeeId==followerId)
            return;
        if (userlist[followerId].followees.contains(followeeId))
            userlist[followerId].followees.remove((Integer) followeeId);
        else return;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值