LC 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence.
The iterator is initialized by RLEIterator(int[] A)
, where A
is a run-length encoding of some sequence. More specifically, for all even i
, A[i]
tells us the number of times that the non-negative integer value A[i+1]
is repeated in the sequence.
The iterator supports one function: next(int n)
, which exhausts the next n
elements (n >= 1
) and returns the last element exhausted in this way. If there is no element left to exhaust, next
returns -1
instead.
For example, we start with A = [3,8,0,9,2,5]
, which is a run-length encoding of the sequence [8,8,8,5,5]
. This is because the sequence can be read as "three eights, zero nines, two fives".
Example 1:
Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
Output: [null,8,8,5,-1]
Explanation:
RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]).
This maps to the sequence [8,8,8,5,5].
RLEIterator.next is then called 4 times: .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5,
but the second term did not exist. Since the last term exhausted does not exist, we return -1.
Note:
0 <= A.length <= 1000
A.length
is an even integer.0 <= A[i] <= 10^9
- There are at most
1000
calls toRLEIterator.next(int n)
per test case. - Each call to
RLEIterator.next(int n)
will have1 <= n <= 10^9
.
Runtime: 95 ms, faster than 82.10% of Java online submissions for RLE Iterator.
class RLEIterator {
private Queue<Integer> q = new ArrayDeque<>();
private int cnt = -;
private int value = -;
public RLEIterator(int[] A) {
for(int i=; i<A.length; i++){
q.add(A[i]);
}
} public int next(int n) {
if(cnt == -){
if(q.isEmpty()) {
return -;
}else {
cnt = q.peek();q.poll();
value = q.peek(); q.poll();
}
}
if(cnt >= n){
cnt -= n;
return value;
}else {
n -= cnt;
cnt = -;
}
while(!q.isEmpty()){
cnt = q.peek(); q.poll();
value = q.peek(); q.poll();
if(cnt >= n) {
cnt -= n;
return value;
}else{
n -= cnt;
cnt = -;
}
}
return -;
}
}
LC 900. RLE Iterator的更多相关文章
- [LeetCode] 900. RLE Iterator RLE迭代器
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- leetcode 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- 【LeetCode】900. RLE Iterator 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rle-itera ...
- 【leetcode】900. RLE Iterator
题目如下: 解题思路:非常简单的题目,直接递归就行了. 代码如下: class RLEIterator(object): def __init__(self, A): ""&quo ...
- [Swift]LeetCode900. RLE 迭代器 | RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- RLE Iterator LT900
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 3.IOC的配置与应用(annotation的方式)
自动装载 @Autowired public class UserService { private UserDAO userDAO; public UserDAO getUserDAO() { re ...
- sql 存储过程笔记3
16:22 2014/1/26一.定义变量--简单赋值declare @a int set @a = 5 print @a --使用select语句赋值declare @user1 nvarchar( ...
- ppp协议解析二
转:http://blog.csdn.net/yangzheng_yz/article/details/11526747 PPP(Point to Point Protocol,点对点协议)协议是为在 ...
- java线程基础巩固---采用多线程方式模拟银行排队叫号以及Runnable接口存在的必要性
采用多线程模拟银行排队叫号: 关于银行拿排队号去叫号的过程我想不必过多解释了,就是有几个业务窗口,并行的处理业务,每处里完一个人,则会叫下一个排队的号去处理业务,一个人是不会被多个窗口工作人员叫号的, ...
- java中的集合总结
知识点: 集合框架和List.set.Map相关集合特点的描述 Collection接口常用方法,List中相对Collection新增的方法,Collection的遍历(一般for循环,增强for循 ...
- freemodbus收藏学习网址
https://www.cnblogs.com/axinno1/p/8521481.html https://blog.csdn.net/xukai871105/article/details/216 ...
- .net 密码明文传输 加密传递方法
未加密传递是这样的 html标签加密使用的是jquery.md5.js 自行官网下载 html代码 <head runat="server"> <meta ht ...
- Qt5 使用lambda
c11新特性中加入了lambda表达式,所以Qt 也支持 需在.pro文件中加入 CONFIG += c++11 m_timer = new QTimer(); m_timer->start() ...
- Can't connect to MySQL server on xxx (10061)
报错原因,数据库服务没有启动,在JDBC连接mysql数据库时会报错 解决方式,在服务中启用Mysql 备注:运行环境: windows10 x64 JDK 1.8.0_181 mysql-conne ...
- OFDM发端硬件实现原理图
OFDM时域削峰法的详细说明可参考:https://www.cnblogs.com/achangchang/p/11037498.html