当前位置:网站首页>LeetCode 1656. Design ordered flow
LeetCode 1656. Design ordered flow
2022-07-03 16:44:00 【Daylight629】
1656. Design ordered flow
Yes n individual (id, value) Yes , among id yes 1 To n An integer between ,value Is a string . non-existent id The same two (id, value) Yes .
Design a stream , With arbitrarily Sequential acquisition n individual (id, value) Yes , And when called many times Press id Increasing order Return some values .
Realization OrderedStream class :
OrderedStream(int n)Construct one that can receivenA stream of values , And put the current pointerptrSet to1.String[] insert(int id, String value)Store new
(id, value)Yes . After storage :
- If the stream store has
id = ptrOf(id, value)Yes , Find out fromid = ptrAt the beginning The longest id A sequence of successive increments , and According to the order Back with these id List of associated values . then , takeptrUpdate to the last oneid + 1. - otherwise , Return an empty list .
- If the stream store has
Example :

Input
["OrderedStream", "insert", "insert", "insert", "insert", "insert"]
[[5], [3, "ccccc"], [1, "aaaaa"], [2, "bbbbb"], [5, "eeeee"], [4, "ddddd"]]
Output
[null, [], ["aaaaa"], ["bbbbb", "ccccc"], [], ["ddddd", "eeeee"]]
explain
OrderedStream os= new OrderedStream(5);
os.insert(3, "ccccc"); // Insert (3, "ccccc"), return []
os.insert(1, "aaaaa"); // Insert (1, "aaaaa"), return ["aaaaa"]
os.insert(2, "bbbbb"); // Insert (2, "bbbbb"), return ["bbbbb", "ccccc"]
os.insert(5, "eeeee"); // Insert (5, "eeeee"), return []
os.insert(4, "ddddd"); // Insert (4, "ddddd"), return ["ddddd", "eeeee"]
Tips :
1 <= n <= 10001 <= id <= nvalue.length == 5valueIt's only made up of lowercase letters- Every time you call
insertWill use a uniqueid - Just call
nTimeinsert
Two 、 Method 1
class OrderedStream {
private int ptr = 1;
private final String[] strs;
public OrderedStream(int n) {
strs = new String[n + 1];
}
public List<String> insert(int idKey, String value) {
strs[idKey] = value;
List<String> res = new ArrayList<>();
while (ptr < strs.length && strs[ptr] != null) {
res.add(strs[ptr]);
ptr++;
}
return res;
}
}
/** * Your OrderedStream object will be instantiated and called as such: * OrderedStream obj = new OrderedStream(n); * List<String> param_1 = obj.insert(idKey,value); */
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- 【剑指 Offer】58 - II. 左旋转字符串
- PHP CI (CodeIgniter) log level setting
- The word backspace key cannot delete the selected text, so you can only press Delete
- IDEA-配置插件
- CC2530 common registers for serial communication
- Unreal_ Datatable implements ID self increment and sets rowname
- (Supplement) double pointer topic
- Mongodb installation and basic operation
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- word 退格键删除不了选中文本,只能按delete
猜你喜欢

关于学习Qt编程的好书精品推荐

Deep understanding of grouping sets statements in SQL

CC2530 common registers for port interrupts

线程池执行定时任务

为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”

Détails du contrôle de la congestion TCP | 3. Espace de conception

Explore Cassandra's decentralized distributed architecture

Explore Netease's large-scale automated testing solutions see here see here

Cocos Creator 2.x 自动打包(构建 + 编译)

What material is sa537cl1? Sa537cl1 corresponds to the national standard material
随机推荐
Develop team OKR in the way of "crowdfunding"
Overview of satellite navigation system
Custom plug-in construction and use of QT plug-in
斑马识别成狗,AI犯错的原因被斯坦福找到了
[Jianzhi offer] 57 - ii Continuous positive sequence with sum s
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
数据分析必备的能力
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
2022.02.14_ Daily question leetcode five hundred and forty
數據分析必備的能力
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
PHP二级域名session共享方案
Acwing game 58
【剑指 Offer 】64. 求1+2+…+n
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
[combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
IDEA-配置插件
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR