Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
舒克儿不开飞机
Fucking Algorithm
提交
9606d343
F
Fucking Algorithm
项目概览
舒克儿不开飞机
/
Fucking Algorithm
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
Fucking Algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9606d343
编写于
11月 11, 2020
作者:
V
Victor Wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
为设计Twitter一文,实现C++版本
严格遵守框架。时间戳改成全局变量,并分模块拆开写(参照Java思路)
上级
8b8f4135
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
119 addition
and
2 deletion
+119
-2
数据结构系列/设计Twitter.md
数据结构系列/设计Twitter.md
+119
-2
未找到文件。
数据结构系列/设计Twitter.md
浏览文件 @
9606d343
...
...
@@ -302,4 +302,121 @@ PS:本文前两张图片和 GIF 是我第一次尝试用平板的绘图软件
<img
src=
"../pictures/qrcode.jpg"
width=
200
>
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
[
happy-yuxuan
](
https://github.com/happy-yuxuan
)
提供 C++ 代码:
```
c++
static
int
timestamp
=
0
;
class
Tweet
{
private:
int
id
;
int
time
;
public:
Tweet
*
next
;
// id为推文内容,time为发文时间
Tweet
(
int
id
,
int
time
)
{
this
->
id
=
id
;
this
->
time
=
time
;
next
=
nullptr
;
}
int
getId
()
const
{
return
this
->
id
;
}
int
getTime
()
const
{
return
this
->
time
;
}
};
class
User
{
private:
int
id
;
public:
Tweet
*
head
;
// 发布的Twitter,用链表表示
unordered_set
<
int
>
followed
;
// 用户关注了那些人
User
(
int
userId
)
{
this
->
id
=
userId
;
head
=
nullptr
;
// 要先把自己关注了
followed
.
insert
(
id
);
}
void
follow
(
int
userId
)
{
followed
.
insert
(
userId
);
}
void
unfollow
(
int
userId
)
{
// 不可以取关自己
if
(
userId
!=
this
->
id
)
followed
.
erase
(
userId
);
}
void
post
(
int
contentId
)
{
Tweet
*
twt
=
new
Tweet
(
contentId
,
timestamp
);
timestamp
++
;
// 将新建的推文插入链表头
// 越靠前的推文 timestamp 值越大
twt
->
next
=
head
;
head
=
twt
;
}
};
class
Twitter
{
private:
// 映射将 userId 和 User 对象对应起来
unordered_map
<
int
,
User
*>
userMap
;
// 判断该用户存不存在系统中,即userMap中存不存在id
inline
bool
contain
(
int
id
)
{
return
userMap
.
find
(
id
)
!=
userMap
.
end
();
}
public:
Twitter
()
{
userMap
.
clear
();
}
/* user 发表一条 tweet 动态 */
void
postTweet
(
int
userId
,
int
tweetId
)
{
if
(
!
contain
(
userId
))
userMap
[
userId
]
=
new
User
(
userId
);
userMap
[
userId
]
->
post
(
tweetId
);
}
/* 返回该 user 关注的人(包括他自己)最近的动态 id,
最多 10 条,而且这些动态必须按从新到旧的时间线顺序排列。*/
vector
<
int
>
getNewsFeed
(
int
userId
)
{
vector
<
int
>
ret
;
if
(
!
contain
(
userId
))
return
ret
;
// 构造一个自动通过Tweet发布的time属性从大到小排序的二叉堆
typedef
function
<
bool
(
const
Tweet
*
,
const
Tweet
*
)
>
Compare
;
Compare
cmp
=
[](
const
Tweet
*
a
,
const
Tweet
*
b
)
{
return
a
->
getTime
()
<
b
->
getTime
();
};
priority_queue
<
Tweet
*
,
vector
<
Tweet
*>
,
Compare
>
q
(
cmp
);
// 关注列表的用户Id
unordered_set
<
int
>
&
users
=
userMap
[
userId
]
->
followed
;
// 先将所有链表头节点插入优先级队列
for
(
int
id
:
users
)
{
if
(
!
contain
(
id
))
continue
;
Tweet
*
twt
=
userMap
[
id
]
->
head
;
if
(
twt
==
nullptr
)
continue
;
q
.
push
(
twt
);
}
while
(
!
q
.
empty
())
{
Tweet
*
t
=
q
.
top
();
q
.
pop
();
ret
.
push_back
(
t
->
getId
());
if
(
ret
.
size
()
==
10
)
return
ret
;
// 最多返回 10 条就够了
if
(
t
->
next
)
q
.
push
(
t
->
next
);
}
return
ret
;
}
/* follower 关注 followee */
void
follow
(
int
followerId
,
int
followeeId
)
{
// 若 follower 不存在,则新建
if
(
!
contain
(
followerId
))
userMap
[
followerId
]
=
new
User
(
followerId
);
// 若 followee 不存在,则新建
if
(
!
contain
(
followeeId
))
userMap
[
followeeId
]
=
new
User
(
followeeId
);
userMap
[
followerId
]
->
follow
(
followeeId
);
}
/* follower 取关 followee,如果 Id 不存在则什么都不做 */
void
unfollow
(
int
followerId
,
int
followeeId
)
{
if
(
contain
(
followerId
))
userMap
[
followerId
]
->
unfollow
(
followeeId
);
}
};
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录