Add Biweekly 39 / weekly 215 solutions

This commit is contained in:
YDZ
2020-11-15 22:39:49 +08:00
parent 7d7007c6e3
commit 91e1a92cdd
8 changed files with 434 additions and 0 deletions

View 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);
*/

View 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)
}