[leetcode]355. Design Twitter设计实现一个微博系统
//先定义一个数据结构,代表一条微博,有两个内容:发布者id,微博id(代表微博内容)
class TwitterData
{
int userId;
int twitterId;
public TwitterData(int userId,int twitterId)
{
this.twitterId = twitterId;
this.userId = userId;
}
}
//用一个map存储用户和各用户的关注用户
Map<Integer,Set<Integer>> userManager ;
//用一个linkedList来存储所有微博
List<TwitterData> twitterDatas ;
/** Initialize your data structure here. */
public Twitter() {
userManager = new HashMap<>();
twitterDatas= new LinkedList<>();
} /** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
//用户名如果没有注册,那就自动注册
if (!userManager.containsKey(userId))
{
Set<Integer> follows = new HashSet<>();
//这里注意,要想看到自己的微博,也要关注自己
follows.add(userId);
userManager.put(userId,follows);
}
TwitterData t = new TwitterData(userId,tweetId);
//添加到微博列表,每次都是添加到头部,代表是新的
twitterDatas.add(0,t);
} /** 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. */
//这里的意思是显示userId关注的人的前10条微博,而不是userId的微博
public List<Integer> getNewsFeed(int userId) {
List<Integer> res = new ArrayList<>();
//用户如果不存在,那么就不显示
if (userManager.containsKey(userId))
{
Set<Integer> follows = userManager.get(userId);
//计数,只显示10条
int count = 0;
for (TwitterData t:
twitterDatas) {
if (count==10) break;
//只添加关注用户的微博
if (follows.contains(t.userId))
{
res.add(t.twitterId);
count++;
}
}
}
return res;
} /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
//er是主动方,ee是被动方
//考虑两个用户不存在的情况
Set<Integer> follows;
if (!userManager.containsKey(followerId))
{
follows = new HashSet<>();
follows.add(followerId);
userManager.put(followerId,follows);
}
if (!userManager.containsKey(followeeId))
{
follows = new HashSet<>();
follows.add(followeeId);
userManager.put(followeeId,follows);
}
//两者都存在后
follows = userManager.get(followerId);
follows.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&&userManager.containsKey(followerId)&&userManager.containsKey(followeeId))
userManager.get(followerId).remove(followeeId);
}
[leetcode]355. Design Twitter设计实现一个微博系统的更多相关文章
- [LeetCode] 355. Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 355 Design Twitter 设计推特
设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文.你的设计需要支持以下的几个功能: postTweet(userI ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
- [LeetCode] Design Twitter 设计推特
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- leetcode 355 Design Twitte
题目连接 https://leetcode.com/problems/design-twitter Design Twitte Description Design a simplified vers ...
- [LeetCode] 534. Design TinyURL 设计短网址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- 355. Design Twitter
二刷尝试了别的办法,用MAP代表关注列表. 然后不初始化,但是只要有用户被使用,而他又不在MAP里,就把他加进去,然后让他关注自己.. 但是这样做超时了. 问题在于这个题解法太多,有很多不同的情况. ...
随机推荐
- 01_Activity生命周期及传递数据
1. Activity的生命周期: 2. Activity启动另一个Activity,并传递数据: package com.example.activitydemo; import android.a ...
- Spring Cloud 学习 (三) Feign
新建 spring-cloud-eureka-feign-client Module pom <parent> <artifactId>spring-cloud-parent& ...
- k8s 部署 Java 项目
前几天安装了 k8s 并测试了自动伸缩功能(HPA),今天来部署一个简单的 Java 应用到 k8s. 开始之前需要先安装一下 ingress 插件.ingress 作为 k8s 的流量入口,有多种实 ...
- 关于utf-8编码值 [ASIS 2019]Unicorn shop
0x00 前言 这题拿到之后有点懵,后来看了 网上的 wp 更加懵,网上大多数都是直接说 去 compart 搜thousand,然后找个大于1337 的就可以,至于为什么?基本都没有给出解答.于是乎 ...
- flask中的return、过滤器详解
之前吧一直学习flask的时候,一直不明白response是怎么产生,今天是明白了.retrun 哎呀,这个地方看着挺小心的东西, 蕴含的能量可不小啊.今天我详细总结总结. 先来写jinjia2语法 ...
- JavaScript一个一维数组变为两个一维数组
//例如[1,2,3,4,5,6,7,8]变为[[1,2,3,4],[5,6,7,8]] var arr=[1,2,3,4,5,6,7,8]; function fixedArray(arr) { v ...
- Java集合源码分析(九)——HashSet
简介 HashSet就是一个集合,里面不能有重复的元素,元素也是无序的. HashSet其实就是调用了HashMap实现的,所以,它也不是线程安全的. HashSet通过iterator()返回的迭代 ...
- 计算机网络——HTTP知识点整理
1.HTTP简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少.它 ...
- 某宝的微信小程序源码合集
这是我在某宝买的小程序源码合集.能用的我用不到.. 我用的到的有的有没后端.仅供个人参考,严禁侵权或商业用途! 下载地址:https://pan.baidu.com/s/1cQEQ17LdN-7hxD ...
- 定制iview选择器——全选按钮
源码下载:https://github.com/littleOneYuan/c_select 本文索引 效果 template data prop methods watch created 效果 下 ...