mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
Add Biweekly 39 / weekly 215 solutions
This commit is contained in:
42
leetcode/5601/5601. Design an Ordered Stream.go
Normal file
42
leetcode/5601/5601. Design an Ordered Stream.go
Normal file
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type OrderedStream struct {
|
||||
ptr int
|
||||
stream []string
|
||||
}
|
||||
|
||||
func Constructor(n int) OrderedStream {
|
||||
ptr, stream := 1, make([]string, n+1)
|
||||
return OrderedStream{ptr: ptr, stream: stream}
|
||||
}
|
||||
|
||||
func (this *OrderedStream) Insert(id int, value string) []string {
|
||||
this.stream[id] = value
|
||||
res := []string{}
|
||||
fmt.Printf("%v %v %v\n", this.ptr, id, value)
|
||||
if this.ptr == id || this.stream[this.ptr] != "" {
|
||||
res = append(res, this.stream[this.ptr])
|
||||
for i := id + 1; i < len(this.stream); i++ {
|
||||
if this.stream[i] != "" {
|
||||
res = append(res, this.stream[i])
|
||||
} else {
|
||||
this.ptr = i
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(res) > 0 {
|
||||
return res
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
/**
|
||||
* Your OrderedStream object will be instantiated and called as such:
|
||||
* obj := Constructor(n);
|
||||
* param_1 := obj.Insert(id,value);
|
||||
*/
|
21
leetcode/5601/5601. Design an Ordered Stream_test.go
Normal file
21
leetcode/5601/5601. Design an Ordered Stream_test.go
Normal file
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem707(t *testing.T) {
|
||||
obj := Constructor(5)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
param1 := obj.Insert(3, "ccccc")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param1 = obj.Insert(1, "aaaaa")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param1 = obj.Insert(2, "bbbbb")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param1 = obj.Insert(5, "eeeee")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
param1 = obj.Insert(4, "ddddd")
|
||||
fmt.Printf("param_1 = %v obj = %v\n", param1, obj)
|
||||
}
|
Reference in New Issue
Block a user