zoukankan      html  css  js  c++  java
  • [LeetCode] 355. Design Twitter Java

    题目:

    Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:

    1. postTweet(userId, tweetId): Compose a new tweet.
    2. getNewsFeed(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.
    3. follow(followerId, followeeId): Follower follows a followee.
    4. unfollow(followerId, followeeId): Follower unfollows a followee.

    Example:

    Twitter twitter = new Twitter();
    // User 1 posts a new tweet (id = 5).
    twitter.postTweet(1, 5);
    // User 1's news feed should return a list with 1 tweet id -> [5].
    twitter.getNewsFeed(1);
    // User 1 follows user 2.
    twitter.follow(1, 2);
    // User 2 posts a new tweet (id = 6).
    twitter.postTweet(2, 6);
    // User 1's news feed should return a list with 2 tweet ids -> [6, 5].
    // Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
    twitter.getNewsFeed(1);
    // User 1 unfollows user 2.
    twitter.unfollow(1, 2)
    // User 1's news feed should return a list with 1 tweet id -> [5],
    // since user 1 is no longer following user 2.
    twitter.getNewsFeed(1);
    题意及分析:实现一个推特的结构,要求能够发推特,关注其他用户,然后获取当前用户或者其关注用户的最新最多10条新推特。这里我直接使用了一个ArrayList来保存所有用户发表的推特。
    然后一个hashmap来保存当前用户关注的其他用户。需要注意的是在获取推特的时候,需要查看本用户发布的推特。
    代码:
    class News{
        int user;
        int tweet;
        public News(int userId,int tweetId){
            user = userId;
            tweet = tweetId;
        }
    }
    
    public class Twitter {
    
       HashMap<Integer,ArrayList<Integer>> followList; //第一个参数为userId,后一个参数为其关注的人的列表
        ArrayList<News> news;
        /** Initialize your data structure here. */
        public Twitter() {
            followList = new HashMap<>();
            news = new ArrayList<>();
        }
    
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) {
            if(userId==1&&tweetId==6417) System.out.println("here6417");
            if(userId==8&&tweetId==3555) System.out.println("here");
            news.add(new News(userId,tweetId));
        }
    
        /** 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> res = new ArrayList<>();
            ArrayList<Integer> peoples = followList.get((Object)userId);        //找到这个人关注的所有人
            int count=0;
            int length = news.size()-1;
            while(count<10&&length>=0){
                News temp = news.get(length);
                if((peoples!=null&&peoples.contains(temp.user))||userId==temp.user){    //是自己或者关注的推特
                    if(!res.contains(temp.tweet)){      //不包含重复的
                        res.add(temp.tweet);
                        count++;
                    }
                }
                length--;
            }
            return res;
        }
    
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) {
            if(followList.get(followerId)==null){       //没有follower的关注人列表
                ArrayList<Integer> arrayList = new ArrayList<>();
                arrayList.add(followeeId);
                followList.put(followerId,arrayList);
                return;
            }
    
            if(!followList.get(followerId).contains(followeeId)){       //Follower has not followed followee
                followList.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(followList.get(followerId)==null)        //如果没有该用户的关注列表,直接返回
                return;
            if(followList.get(followerId).contains(followeeId)){       //如果Follower关注了followee
                followList.get(followerId).remove((Object)followeeId);
                if(followList.get(followerId).size()==0)        //如果把所有的都删除了
                    followList.remove(followerId);
            }
        }
    }
    
    /**
     * 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);
     */
    View Code
    
    
    
    
    
    
    

     
    Seen this question in a real interview befo
  • 相关阅读:
    C#异步编程
    3.TinkPHP中的模型
    Socket网络编程
    日志查看登录用户
    ssh相关的设置
    爬虫学习笔记
    python升级到3.*版本
    Redis未授权访问攻击过程与防范
    Linux重置MySQL密码
    linux下WordPress安装
  • 原文地址:https://www.cnblogs.com/271934Liao/p/7274386.html
Copyright © 2011-2022 走看看