Skip to content

Commit

Permalink
Resolved 933
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanData committed Oct 13, 2023
1 parent e36f97d commit d5697fc
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lc/lc933NumberofRecentCalls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lc

import "testing"

type RecentCounter struct {
queue []int
}

func Constructor() RecentCounter {
return RecentCounter{
queue: make([]int, 0),
}
}

func (r *RecentCounter) Ping(t int) int {
r.queue = append(r.queue, t)
// 移除所有小於t-3000的請求
for len(r.queue) > 0 && r.queue[0] < t-3000 {
r.queue = r.queue[1:]
}
return len(r.queue)
}

func TestRecentCounter(t *testing.T) {
rc := Constructor()
if got := rc.Ping(1); got != 1 {
t.Errorf("rc.Ping(1) = %d; want 1", got)
}
if got := rc.Ping(100); got != 2 {
t.Errorf("rc.Ping(100) = %d; want 2", got)
}
if got := rc.Ping(3001); got != 3 {
t.Errorf("rc.Ping(3001) = %d; want 3", got)
}
if got := rc.Ping(3002); got != 3 {
t.Errorf("rc.Ping(3002) = %d; want 3", got)
}
}

0 comments on commit d5697fc

Please sign in to comment.