【LetMeFly】1656.设计有序流:阅读理解
力扣题目链接:https://leetcode.cn/problems/design-an-ordered-stream/
有 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"]
TEXT
提示:
1 <= n <= 1000
1 <= id <= n
value.length == 5
value
仅由小写字母组成
- 每次调用
insert
都会使用一个唯一的 id
- 恰好调用
n
次 insert
方法一:构造
类初始化时,开辟一个大小为的空间,并将赋值为
之后每次插入元素时,先将元素放入数组中的对应位置,然后当指针在合法范围内且当前位置不为空时,将这个元素添加到答案中。
- 时间复杂度:
- 初始化的时间复杂度为,因为要开辟大小为的空间
- 之后每次调用的平均复杂度为,因为所有的次调用共访问了数组或次
- 空间复杂度
AC代码
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class OrderedStream { private: vector<string> v; int nowLoc; public: OrderedStream(int n) : nowLoc(1) { v = vector<string>(n + 1); } vector<string> insert(int idKey, string value) { v[idKey] = value; vector<string> ans; while (nowLoc < v.size() && v[nowLoc].size()) { ans.push_back(v[nowLoc++]); } return ans; } };
CPP
|
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| ''' Author: LetMeFly Date: 2025-02-24 09:09:17 LastEditors: LetMeFly.xyz LastEditTime: 2025-02-24 09:11:29 ''' from typing import List
class OrderedStream:
def __init__(self, n: int): self.n = n self.ptr = 1 self.v = [None] * (n + 1)
def insert(self, idKey: int, value: str) -> List[str]: self.v[idKey] = value if idKey != self.ptr: return [] ans = [] while self.ptr <= self.n and self.v[self.ptr]: ans.append(self.v[self.ptr]) self.ptr += 1 return ans
PYTHON
|
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
import java.util.List; import java.util.ArrayList;
class OrderedStream { private int n; private int ptr; private final String[] v;
public OrderedStream(int n) { this.n = n; ptr = 1; v = new String[n + 1]; } public List<String> insert(int idKey, String value) { v[idKey] = value; if (idKey != ptr) { return new ArrayList<>(); } List<String> ans = new ArrayList<>(); while (ptr <= n && v[ptr] != null) { ans.add(v[ptr]); ptr++; } return ans; } }
JAVA
|
Golang:也可使用指针直接引用切片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
package main
type OrderedStream struct { n, ptr int v []string }
func Constructor(n int) OrderedStream { return OrderedStream{ n: n, ptr: 1, v: make([]string, n + 1), } }
func (this *OrderedStream) Insert(idKey int, value string) []string { this.v[idKey] = value start := this.ptr for this.ptr <= this.n && len(this.v[this.ptr]) > 0 { this.ptr++ } return this.v[start:this.ptr] }
GO
|
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126358485