1656.设计有序流(简单)

1656.设计有序流(简单) (opens new window)

解法:模拟

本题含义为从头开始连续输出有值的数组元素,若数组中由位置为空,暂停输出直至空位置被填上。

采用数组存储,记录指针,依次判定即可。

type OrderedStream struct {
	point int
	data  []string
}

func Constructor(n int) OrderedStream {
	return OrderedStream{data: make([]string, n)}
}

func (this *OrderedStream) Insert(idKey int, value string) []string {
	p := idKey - 1
	this.data[p] = value
	st := this.point
	for ; this.point < len(this.data); this.point++ {
		if this.data[this.point] == "" {
			break
		}
	}
	return this.data[st:this.point]
}