顺序遍历哈希表,用长度固定的数组存储字符串,求解《1656. 设计有序流》
有 n 个 (id, value) 对,其中 id 是 1 到 n 之间的一个整数,value 是一个字符串。不存在 id 相同的两个 (id, value) 对。
设计一个流,以 任意 顺序获取 n 个 (id, value) 对,并在多次调用时 按 id 递增的顺序 返回一些值。
实现 OrderedStream 类:
OrderedStream(int n) 构造一个能接收 n 个值的流,并将当前指针 ptr 设为 1 。
String[] insert(int id, String value) 向流中存储新的 (id, value) 对。存储后:
如果流存储有 id = ptr 的 (id, value) 对,则找出从 id = ptr 开始的 最长 id 连续递增序列 ,并 按顺序 返回与这些 id 关联的值的列表。然后,将 ptr 更新为最后那个 id + 1 。
否则,返回一个空列表。
示例:
输入
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
输出
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
解释
OrderedStream os= new OrderedStream(5);
os.insert(3, "ccccc"); // 插入 (3, "ccccc"),返回 []
os.insert(1, "aaaaa"); // 插入 (1, "aaaaa"),返回 ["aaaaa"]
os.insert(2, "bbbbb"); // 插入 (2, "bbbbb"),返回 ["bbbbb", "ccccc"]
os.insert(5, "eeeee"); // 插入 (5, "eeeee"),返回 []
os.insert(4, "ddddd"); // 插入 (4, "ddddd"),返回 ["ddddd", "eeeee"]
var OrderedStream = function(n) { // Map 实现
this.h = new Map
this.n = n
this.ptr = 1
};
OrderedStream.prototype.insert = function(idKey, value) {
this.h.set(idKey, value)
const r = []
while (this.ptr <= this.n && this.h.has(this.ptr)) {
r.push(this.h.get(this.ptr++))
}
return r
};
var OrderedStream = function(n) { // 数组实现
this.h = new Array(n + 1)
this.n = n
this.ptr = 1
};
OrderedStream.prototype.insert = function(idKey, value) {
this.h[idKey] = value
const r = []
while (this.ptr <= this.n && this.h[this.ptr] !== void 0) {
r.push(this.h[this.ptr++])
}
return r
};
var OrderedStream = function(n) { // Object 实现
this.h = Object.create(null)
this.n = n
this.ptr = 1
};
OrderedStream.prototype.insert = function(idKey, value) {
this.h[idKey] = value
const r = []
while (this.ptr <= this.n && this.h[this.ptr] !== void 0) {
r.push(this.h[this.ptr++])
}
return r
};
class OrderedStream {
h: Map<number, string>
n: number
ptr: number
constructor(n: number) {
this.h = new Map
this.n = n
this.ptr = 1
}
insert(idKey: number, value: string): string[] {
this.h.set(idKey, value)
const r: string[] = []
while (this.ptr <= this.n && this.h.has(this.ptr)) {
r.push(this.h.get(this.ptr++))
}
return r
}
}
class OrderedStream {
function __construct($n) {
$this->n = $n;
$this->h = array_fill(0, $n + 1, '');
$this->ptr = 1;
}
function insert($idKey, $value) {
$this->h[$idKey] = $value;
$r = [];
while ($this->ptr <= $this->n && $this->h[$this->ptr] !== '') {
$r []= $this->h[$this->ptr++];
}
return $r;
}
}
type OrderedStream struct {
n int
ptr int
h map[int]string
}
func Constructor(n int) OrderedStream {
return OrderedStream{
n,
1,
make(map[int]string, n + 1),
}
}
func (this *OrderedStream) Insert(idKey int, value string) []string {
this.h[idKey] = value
var r []string
for this.ptr <= this.n && this.h[this.ptr] != "" {
r = append(r, this.h[this.ptr])
this.ptr++
}
return r
}
class OrderedStream {
private int n;
private int ptr;
private String[] h;
public OrderedStream(int n) {
this.n = n;
ptr = 1;
h = new String[n + 1];
}
public List<String> insert(int idKey, String value) {
h[idKey] = value;
List<String> r = new ArrayList<String>();
while (ptr <= n && h[ptr] != null) {
r.add(h[ptr++]);
}
return r;
}
}
public class OrderedStream {
private int n;
private int ptr = 1;
private string[] h;
public OrderedStream(int n) {
this.n = n;
h = new string[n + 1];
}
public IList<string> Insert(int idKey, string value) {
h[idKey] = value;
IList<string> r = new List<string>();
while (ptr <= n && h[ptr] != null) {
r.Add(h[ptr++]);
}
return r;
}
}
typedef struct {
int n;
int ptr;
char** h;
} OrderedStream;
OrderedStream* orderedStreamCreate(int n) {
OrderedStream* obj = malloc(sizeof(OrderedStream));
obj->n = n;
obj->ptr = 1;
obj->h = malloc(sizeof(char*) * (n + 1));
for (int i = 0; i <= n; i++) {
obj->h[i] = NULL;
}
return obj;
}
char ** orderedStreamInsert(OrderedStream* obj, int idKey, char * value, int* retSize) {
obj->h[idKey] = value;
char** r = malloc(sizeof(char*) * obj->n);
int pos = 0;
while (obj->ptr <= obj->n && obj->h[obj->ptr] != NULL) {
r[pos++] = obj->h[obj->ptr++];
}
*retSize = pos;
return r;
}
void orderedStreamFree(OrderedStream* obj) {
free(obj->h);
free(obj);
}
class OrderedStream {
private:
int n;
int ptr;
vector<string> h;
public:
OrderedStream(int n) {
this->n = n;
ptr = 1;
h = vector<string>(n + 1, "");
}
vector<string> insert(int idKey, string value) {
h[idKey] = value;
vector<string> r;
while (ptr <= n && h[ptr] != "") {
r.push_back(h[ptr++]);
}
return r;
}
};
class OrderedStream:
def __init__(self, n: int):
self.n, self.ptr, self.h = n, 1, [''] * (n + 1)
def insert(self, idKey: int, value: str) -> List[str]:
self.h[idKey] = value
r = []
while self.ptr <= self.n and self.h[self.ptr] != '':
r.append(self.h[self.ptr])
self.ptr += 1
return r