当前位置:网站首页>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 receiven
A stream of values , And put the current pointerptr
Set to1
.String[] insert(int id, String value)
Store new
(id, value)
Yes . After storage :
- If the stream store has
id = ptr
Of(id, value)
Yes , Find out fromid = ptr
At the beginning The longest id A sequence of successive increments , and According to the order Back with these id List of associated values . then , takeptr
Update 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 <= 1000
1 <= id <= n
value.length == 5
value
It's only made up of lowercase letters- Every time you call
insert
Will use a uniqueid
- Just call
n
Timeinsert
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).
边栏推荐
- utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
- Svn usage specification
- 爱可可AI前沿推介(7.3)
- SVN使用规范
- 程序猿如何快速成长
- 【剑指 Offer】58 - I. 翻转单词顺序
- Unity project optimization case 1
- [statement] about searching sogk1997 and finding many web crawler results
- Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
- Record windows10 installation tensorflow-gpu2.4.0
猜你喜欢
Visual SLAM algorithms: a survey from 2010 to 2016
Cocos Creator 2.x 自动打包(构建 + 编译)
Mysql database -dql
ThreeJS 第二篇:顶点概念、几何体结构
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
CC2530 common registers for port initialization
What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr
CC2530 common registers for port interrupts
随机推荐
SVN使用规范
利用MySQL中的乐观锁和悲观锁实现分布式锁
QT serial port UI design and solution to display Chinese garbled code
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
2022爱分析· 国央企数字化厂商全景报告
(Supplement) double pointer topic
ThreeJS 第二篇:顶点概念、几何体结构
CC2530 common registers
nifi从入门到实战(保姆级教程)——flow
Aike AI frontier promotion (7.3)
关于学习Qt编程的好书精品推荐
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
RF Analyze Demo搭建 Step by Step
【剑指 Offer 】57 - II. 和为s的连续正数序列
What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
Overview of satellite navigation system
Détails du contrôle de la congestion TCP | 3. Espace de conception
Golang anonymous function use
Pointcut expression