当前位置: 首页 > news >正文

浙江省城乡建设网站适合新手做的网站静态

浙江省城乡建设网站,适合新手做的网站静态,担保公司网站建设方案,电商设计需要学什么软件有哪些【问题描述】355 设计推特 设计一个简化版的推特(Twitter)#xff0c;可以让用户实现发送推文#xff0c;关注/取消关注其他用户#xff0c;能够看见关注人#xff08;包括自己#xff09;的最近十条推文。你的设计需要支持以下的几个功能#xff1a;postTweet(userId, …【问题描述】355 设计推特 设计一个简化版的推特(Twitter)可以让用户实现发送推文关注/取消关注其他用户能够看见关注人包括自己的最近十条推文。你的设计需要支持以下的几个功能postTweet(userId, tweetId): 创建一条新的推文 getNewsFeed(userId): 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。 follow(followerId, followeeId): 关注一个用户 unfollow(followerId, followeeId): 取消关注一个用户 示例:Twitter twitter new Twitter();// 用户1发送了一条新推文 (用户id 1, 推文id 5). twitter.postTweet(1, 5);// 用户1的获取推文应当返回一个列表其中包含一个id为5的推文. twitter.getNewsFeed(1); // 用户1关注了用户2. twitter.follow(1, 2); // 用户2发送了一个新推文 (推文id 6). twitter.postTweet(2, 6); // 用户1的获取推文应当返回一个列表其中包含两个推文id分别为 - [6, 5]. // 推文id6应当在推文id5之前因为它是在5之后发送的. twitter.getNewsFeed(1); // 用户1取消关注了用户2. twitter.unfollow(1, 2); // 用户1的获取推文应当返回一个列表其中包含一个id为5的推文. // 因为用户1已经不再关注用户2. twitter.getNewsFeed(1);【解答思路】 1. 设计思路 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Set;public class Twitter {/*** 用户 id 和推文单链表的对应关系*/private MapInteger, Tweet twitter;/*** 用户 id 和他关注的用户列表的对应关系*/private MapInteger, SetInteger followings;/*** 全局使用的时间戳字段用户每发布一条推文之前 1*/private static int timestamp 0;/*** 合并 k 组推文使用的数据结构可以在方法里创建使用声明成全局变量非必需视个人情况使用*/private static PriorityQueueTweet maxHeap;/*** Initialize your data structure here.*/public Twitter() {followings new HashMap();twitter new HashMap();maxHeap new PriorityQueue((o1, o2) - -o1.timestamp o2.timestamp);}/*** Compose a new tweet.*/public void postTweet(int userId, int tweetId) {timestamp;if (twitter.containsKey(userId)) {Tweet oldHead twitter.get(userId);Tweet newHead new Tweet(tweetId, timestamp);newHead.next oldHead;twitter.put(userId, newHead);} else {twitter.put(userId, new Tweet(tweetId, timestamp));}}/*** Retrieve the 10 most recent tweet ids in the users 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 ListInteger getNewsFeed(int userId) {// 由于是全局使用的使用之前需要清空maxHeap.clear();// 如果自己发了推文也要算上if (twitter.containsKey(userId)) {maxHeap.offer(twitter.get(userId));}SetInteger followingList followings.get(userId);if (followingList ! null followingList.size() 0) {for (Integer followingId : followingList) {Tweet tweet twitter.get(followingId);if (tweet ! null) {maxHeap.offer(tweet);}}}ListInteger res new ArrayList(10);int count 0;while (!maxHeap.isEmpty() count 10) {Tweet head maxHeap.poll();res.add(head.id);// 这里最好的操作应该是 replace但是 Java 没有提供if (head.next ! null) {maxHeap.offer(head.next);}count;}return res;}/*** Follower follows a followee. If the operation is invalid, it should be a no-op.** param followerId 发起关注者 id* param followeeId 被关注者 id*/public void follow(int followerId, int followeeId) {// 被关注人不能是自己if (followeeId followerId) {return;}// 获取我自己的关注列表SetInteger followingList followings.get(followerId);if (followingList null) {SetInteger init new HashSet();init.add(followeeId);followings.put(followerId, init);} else {if (followingList.contains(followeeId)) {return;}followingList.add(followeeId);}}/*** Follower unfollows a followee. If the operation is invalid, it should be a no-op.** param followerId 发起取消关注的人的 id* param followeeId 被取消关注的人的 id*/public void unfollow(int followerId, int followeeId) {if (followeeId followerId) {return;}// 获取我自己的关注列表SetInteger followingList followings.get(followerId);if (followingList null) {return;}// 这里删除之前无需做判断因为查找是否存在以后就可以删除反正删除之前都要查找followingList.remove(followeeId);}/*** 推文类是一个单链表结点视角*/private class Tweet {/*** 推文 id*/private int id;/*** 发推文的时间戳*/private int timestamp;private Tweet next;public Tweet(int id, int timestamp) {this.id id;this.timestamp timestamp;}}public static void main(String[] args) {Twitter twitter new Twitter();twitter.postTweet(1, 1);ListInteger res1 twitter.getNewsFeed(1);System.out.println(res1);twitter.follow(2, 1);ListInteger res2 twitter.getNewsFeed(2);System.out.println(res2);twitter.unfollow(2, 1);ListInteger res3 twitter.getNewsFeed(2);System.out.println(res3);}作者liweiwei1419 链接https://leetcode-cn.com/problems/design-twitter/solution/ha-xi-biao-lian-biao-you-xian-dui-lie-java-by-liwe/ 【总结】 1.维护数据 如果需要维护数据的时间有序性链表在特殊场景下可以胜任。因为时间属性通常来说是相对固定的而不必去维护顺序性如果需要动态维护数据有序性「优先队列」堆是可以胜任的 2. maxHeap new PriorityQueue((o1, o2) - -o1.timestamp o2.timestamp); 中的(o1, o2) - -o1.timestamp o2.timestamp 这里是 Java 的 lambda 表达式等价于Java 代码maxHeap new PriorityQueue(new ComparatorTweet() {Overridepublic int compare(Tweet o1, Tweet o2) {return -o1.timestamp o2.timestamp;} }); lambda 表达式它是把函数作为一个方法的参数的一种等价写法可以认为是「语法糖」。在 IDEA 工具里面写匿名方法然后再让 IDEA 帮助优化成 lambda 表达式。3. 面向对象 注意细节 分级思考 想好再码 原文链接;https://leetcode-cn.com/problems/design-twitter/solution/ha-xi-biao-lian-biao-you-xian-dui-lie-java-by-liwe/
http://www.huolong8.cn/news/401457/

相关文章:

  • 建设部网站招标投标文件房地产网站设计方案
  • 模板网站的缺点100个免费设计网站
  • 网站建设面试表做购物网站哪种服务器好
  • 南和邢台网站制作有没有设计房子的软件
  • 朔州公司做网站建筑网官网登录入口
  • 记事本做网站的代码word 发布wordpress
  • 网站 易用性原则苏州网站建设网
  • asp下载网站代码wordpress 主题 制作视频教程
  • 网站优化怎么做分录wordpress 首页导航
  • 建设专业网站wordpress自带阅读数
  • 建设营销型网站流程图电子商城网站建设与维护
  • 北京一家专门做会所的网站豆瓣网站是怎么建设的
  • 网站打开的速度特别慢的原因徐州三华网架公司
  • 枣庄建设路小学网站网络营销期末总结
  • 河南响应式建站网站的效果图
  • 专业集团网站建设嘉上营销
  • 铁岭做网站包括哪些wordpress 网站很慢
  • 餐饮网站建设规划书企业网站模板html
  • 南宁免费网站建站模板怎样使wordpress网站文章左对齐
  • 公司建站方案网页设计代码html分行
  • 网站建设的架构女生学建筑工程技术就业前景
  • 淄博网站制作定制改版招商网站开发
  • 网站设计风格及特点网站开发手机验证码
  • 织梦做的网站怎样wordpress会员注册模板
  • 做网站公司哪里好做网站要备案
  • 网站开发成本会计科目wordpress中title
  • wordpress站点标题美化开发网站建设公司
  • 南宁做网站设计方案差异基因做热图在线网站
  • 动态表情包在线制作网站怎么样建立一个网站
  • 住房和城乡建设部网站造价师管理咨询公司起名字