zoukankan      html  css  js  c++  java
  • 355. Design Twitter

    二刷尝试了别的办法,用MAP<Integer,set>代表关注列表。
    然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己。。

    但是这样做超时了。

    问题在于这个题解法太多,有很多不同的情况。

    STACK记录所有推,然后找10个,那太难了,可能某个用户关注的只是几亿里面的某些人,搜索起来很要命。

    那可以某人发推,就给所有关注他的人推一下。A关注B的时候,A刷新自己的推特队列,按时间顺序重新查找。这样比较符合现状,但是假如我有几亿关注者,每发一个就要刷新所有人的列表,这个还好。但是,每多一个人关注我,他的列表就要重新构建,按照时间,那就要遍历他关注的所有人的所有推,也很要命。。

    另外数组初始问题在现实不需要纠结。。谁注册给谁ID就行了。。。实际中不会存在后台接到了没注册ID账户的操作请求。。。

    总之。。这个题就是各种tradeoff,给的限定条件越多越容易接近最好设计方案。如果面试,希望是面试官给出设计,自己能距离说出这种设计的利弊,否则基本给个设计方案都能指出不适用的条件。

    public class Twitter 
    {
        Stack<Integer> tweets = new Stack<>();
        Map<Integer,Integer> tweetsMap = new HashMap<Integer,Integer>();
        List<Set<Integer>> users = new ArrayList<Set<Integer>>();
    
    
        /** Initialize your data structure here. */
        public Twitter() 
        {
            for(int i = 0; i < 1000;i++)
            {
                Set<Integer> tempSet = new HashSet<Integer>(1000);
                tempSet.add(i);
                users.add(new HashSet<Integer>(tempSet));
            }
        }
        
        /** Compose a new tweet. */
        public void postTweet(int userId, int tweetId) 
        {
    
            tweets.push(tweetId);
            tweetsMap.put(tweetId,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> res = new ArrayList<>();
            Stack<Integer> tempStk = new Stack<Integer>();
            
            while(res.size() < 10)
            {
                if(tweets.size()==0) break;
                int temptweetID = tweets.pop();
                tempStk.push(temptweetID);
                if(users.get(userId).contains(tweetsMap.get(temptweetID))) res.add(temptweetID);
                
                
            }
            
            while(!tempStk.isEmpty())
            {
                tweets.push(tempStk.pop());
            }
            return res;
        }
        
        /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
        public void follow(int followerId, int followeeId) 
        {
    
                users.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(followerId != followeeId)
                users.get(followerId).remove(followeeId);
            
        }
    }
    

    这个初始4000人的就很蠢。。自己都想抽自己。假如有10个用户就浪费了,有4001个用户就报错了。
    但是不初始4000人,每次有相关用户活动都要检测是否存在,不存在就加超时了。其实我就想知道,没注册的用户哪来的ID,怎么去给服务器发操作请求。

  • 相关阅读:
    HDU4628+状态压缩DP
    Javascript 去掉字符串前后空格的五种方法
    Javascript 数组之判断取值和数组取值
    ASP.NET MVC 出现错误 “The view 'XXX' or its master was not found or no view engine support”
    ASP.NET MVC 页面调整并传递参数
    ASP.NET MV3 部署网站 报"Could not load file or assembly ' System.Web.Helpers “ 错的解决方法
    ASP.NET MVC 控制器向View传值的三种方法
    CSharp 如何通过拼接XML调用存储过程来查询数据
    SQLServer : EXEC和sp_executesql的区别
    关于SQLServer2005的学习笔记—异常捕获及处理
  • 原文地址:https://www.cnblogs.com/reboot329/p/5875843.html
Copyright © 2011-2022 走看看