Skip to content

Commit c9ea3fb

Browse files
committed
系统: 推特
Change-Id: I5193f8b19c16857493e70ac60bc1486765f783fb
1 parent b1be705 commit c9ea3fb

File tree

3 files changed

+198
-26
lines changed

3 files changed

+198
-26
lines changed

go/leetcode/355.设计推特.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* @lc app=leetcode.cn id=355 lang=golang
3+
*
4+
* [355] 设计推特
5+
*
6+
* https://leetcode-cn.com/problems/design-twitter/description/
7+
*
8+
* algorithms
9+
* Medium (33.96%)
10+
* Likes: 39
11+
* Dislikes: 0
12+
* Total Accepted: 1.7K
13+
* Total Submissions: 4.9K
14+
* Testcase Example: '["Twitter","postTweet","getNewsFeed","follow","postTweet","getNewsFeed","unfollow","getNewsFeed"]\n[[],[1,5],[1],[1,2],[2,6],[1],[1,2],[1]]'
15+
*
16+
*
17+
* 设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。你的设计需要支持以下的几个功能:
18+
*
19+
*
20+
* postTweet(userId, tweetId): 创建一条新的推文
21+
* getNewsFeed(userId):
22+
* 检索最近的十条推文。每个推文都必须是由此用户关注的人或者是用户自己发出的。推文必须按照时间顺序由最近的开始排序。
23+
* follow(followerId, followeeId): 关注一个用户
24+
* unfollow(followerId, followeeId): 取消关注一个用户
25+
*
26+
*
27+
* 示例:
28+
*
29+
*
30+
* Twitter twitter = new Twitter();
31+
*
32+
* // 用户1发送了一条新推文 (用户id = 1, 推文id = 5).
33+
* twitter.postTweet(1, 5);
34+
*
35+
* // 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
36+
* twitter.getNewsFeed(1);
37+
*
38+
* // 用户1关注了用户2.
39+
* twitter.follow(1, 2);
40+
*
41+
* // 用户2发送了一个新推文 (推文id = 6).
42+
* twitter.postTweet(2, 6);
43+
*
44+
* // 用户1的获取推文应当返回一个列表,其中包含两个推文,id分别为 -> [6, 5].
45+
* // 推文id6应当在推文id5之前,因为它是在5之后发送的.
46+
* twitter.getNewsFeed(1);
47+
*
48+
* // 用户1取消关注了用户2.
49+
* twitter.unfollow(1, 2);
50+
*
51+
* // 用户1的获取推文应当返回一个列表,其中包含一个id为5的推文.
52+
* // 因为用户1已经不再关注用户2.
53+
* twitter.getNewsFeed(1);
54+
*
55+
*
56+
*/
57+
58+
// @lc code=start
59+
import (
60+
"container/heap"
61+
)
62+
63+
type Tweet struct {
64+
Id int
65+
Index int
66+
}
67+
68+
type TweetHeap []Tweet
69+
70+
func (h *TweetHeap) Less(i, j int) bool {
71+
return (*h)[i].Index < (*h)[j].Index
72+
}
73+
74+
func (h *TweetHeap) Swap(i, j int) {
75+
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
76+
}
77+
78+
func (h *TweetHeap) Len() int {
79+
return len(*h)
80+
}
81+
82+
func (h *TweetHeap) Pop() (v interface{}) {
83+
*h, v = (*h)[:h.Len()-1], (*h)[h.Len()-1]
84+
return
85+
}
86+
87+
func (h *TweetHeap) Push(v interface{}) {
88+
*h = append(*h, v.(Tweet))
89+
}
90+
91+
type Twitter struct {
92+
FollowMap map[int]map[int]int // uid: map{uid: 1}
93+
TweetMap map[int][]Tweet // uid: tweet
94+
MaxIndex int
95+
}
96+
97+
/** Initialize your data structure here. */
98+
func Constructor() Twitter {
99+
return Twitter{
100+
FollowMap: map[int]map[int]int{},
101+
TweetMap: map[int][]Tweet{},
102+
MaxIndex: 0,
103+
}
104+
}
105+
106+
/** Compose a new tweet. */
107+
func (this *Twitter) PostTweet(userId int, tweetId int) {
108+
this.MaxIndex++
109+
index := this.MaxIndex
110+
tweet := Tweet{Id: tweetId, Index: index}
111+
if _, ok := this.TweetMap[userId]; !ok {
112+
this.TweetMap[userId] = []Tweet{}
113+
}
114+
this.TweetMap[userId] = append(this.TweetMap[userId], tweet)
115+
}
116+
117+
/** 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. */
118+
func (this *Twitter) GetNewsFeed(userId int) []int {
119+
th := make(TweetHeap, 0)
120+
heap.Init(&th)
121+
limit := 10 // 限制10条
122+
uids := []int{userId}
123+
for uid := range this.FollowMap[userId] {
124+
if uid != userId {
125+
uids = append(uids, uid)
126+
}
127+
}
128+
for _, uid := range uids {
129+
tweets := this.TweetMap[uid]
130+
for _, el := range tweets {
131+
if th.Len() >= limit {
132+
if th[0].Index >= el.Index {
133+
continue // 发表时间比堆顶小,跳过
134+
}
135+
heap.Pop(&th)
136+
}
137+
// 判断堆头
138+
heap.Push(&th, el)
139+
}
140+
}
141+
rev := []int{}
142+
for th.Len() > 0 {
143+
rev = append(rev, heap.Pop(&th).(Tweet).Id)
144+
}
145+
ret := make([]int, 0)
146+
for i := len(rev) - 1; i >= 0; i-- {
147+
ret = append(ret, rev[i])
148+
}
149+
return ret
150+
}
151+
152+
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
153+
func (this *Twitter) Follow(followerId int, followeeId int) {
154+
if _, ok := this.FollowMap[followerId]; !ok {
155+
this.FollowMap[followerId] = map[int]int{}
156+
}
157+
this.FollowMap[followerId][followeeId] = 1
158+
}
159+
160+
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
161+
func (this *Twitter) Unfollow(followerId int, followeeId int) {
162+
delete(this.FollowMap[followerId], followeeId)
163+
}
164+
165+
/**
166+
* Your Twitter object will be instantiated and called as such:
167+
* obj := Constructor();
168+
* obj.PostTweet(userId,tweetId);
169+
* param_2 := obj.GetNewsFeed(userId);
170+
* obj.Follow(followerId,followeeId);
171+
* obj.Unfollow(followerId,followeeId);
172+
*/
173+
// @lc code=end
174+

go/leetcode/4.median-of-two-sorted-arrays.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func findNth(arr1 []int, arr2 []int, n int) int {
117117
} else if m2 > l2-1 {
118118
arr1 = leftSub(arr1, m1+1)
119119
n = n - m1 - 1
120-
// 下面讨论k/2都在数组中的情况,这时肯定不可能在最小端
120+
// 下面讨论k/2都在数组中的情况,这时肯定不可能在最小端
121121
} else if arr1[m1] < arr2[m2] {
122122
arr1 = leftSub(arr1, m1+1)
123123
n = n - m1 - 1

go/main.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package main
22

33
import (
44
"fmt"
5+
"log"
6+
"math/rand"
7+
"net/http"
58
_ "net/http/pprof"
9+
"runtime"
10+
"time"
611
// "math"
712
// "strconv"
813
// "strings"
@@ -32,36 +37,29 @@ func main() {
3237
// fmt.Println(obj.Get(2)) // 返回 -1 (未找到)
3338
// fmt.Println(obj.Get(3)) // 返回 33
3439

35-
// n := runtime.NumCPU()
36-
// fmt.Println("cpu num=", n)
37-
// runtime.GOMAXPROCS(n)
40+
n := runtime.NumCPU()
41+
fmt.Println("cpu num=", n)
42+
runtime.GOMAXPROCS(n)
3843

39-
// go func() {
40-
// log.Println(http.ListenAndServe("localhost:10000", nil))
41-
// }()
44+
go func() {
45+
log.Println(http.ListenAndServe("localhost:10000", nil))
46+
}()
4247

43-
// fmt.Println("stage 0, go num=", runtime.NumGoroutine()) // 默认两个go
44-
45-
// // AlternateOutputViaChannel()
46-
// // AlternateOutputViaAtomic()
48+
// AlternateOutputViaChannel()
49+
// AlternateOutputViaAtomic()
4750
// base.AlternateOutputViaCond()
48-
49-
// time.Sleep(100 * time.Second)
50-
51-
// fmt.Println(helper([]int{1, 2, 3}))
52-
// fmt.Println(myAtoi("42"))
53-
// fmt.Println(multiply("456", "123"))
54-
// strings.IndexOf()
55-
a := [][]int{
56-
[]int{5, 1, 9,11},
57-
[]int{2, 4, 8,10},
58-
[]int{13, 3, 6, 7},
59-
[]int{15,14,12,16},
51+
for i := 0; i < 32; i++ {
52+
go func() {
53+
for {
54+
rand.Intn(1e9)
55+
}
56+
}()
6057
}
61-
rotate(a)
62-
fmt.Println(a)
58+
fmt.Println("stage 0, go num=", runtime.NumGoroutine()) // 默认两个go
59+
60+
time.Sleep(100 * time.Second)
6361

64-
fmt.Println("===== end =====")
62+
// fmt.Println("===== end =====")
6563
}
6664

6765
func rotate(matrix [][]int) {

0 commit comments

Comments
 (0)