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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Spring Cloud(O)服务的注册与发现(Eureka)
一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通过负载均衡设备共同对外提供服务 1.3 ...
- mysql 5.6.38 数据库编译安装
一.系统环境: # cat /etc/redhat-release CentOS release 6.9 (Final) 二.mysql 编译安装: 1.安装依赖包: yum install -y n ...
- 牛客练习赛48 E 小w的矩阵前k大元素
E 思路: 优先队列,将迭代器变量作为结构体的变量. 迭代器走的时候只能像一个方向走,另外一个方向只有最开始才走.如下图所示: 如果两个方向同时走,同一个值会被遍历多次,像上图那样就能保证每个位置都走 ...
- layui 单选框、多选框radio 元素判断是必填项 lay-verify='required'
简单验证复选框,单选框必填 例子代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- inferiors
inferiors 英[ɪnˈfɪərɪəz] 美[ɪnˈfɪriərz] n. 不如别人的人; 级别(或地位)低的人; [词典] inferior的复数; info inferiors
- 16-SQLServer强制走索引
一.注意点 1.使用with(index(索引名称))来使SQL强制走索引. 二.示例截图 1.创建非聚集索引 2.不使用with,不走索引的截图 3.使用with,强制走索引的截图
- RHEL6 学习:使用 cryptsetup 给分区加密
RHEL6 学习:使用 cryptsetup 给分区加密 今天学习了 RHEL 对硬盘分区加密的知识,在 RHEL 系统里可以通过使用 cryptsetup 工具对硬盘分区进行加密,加密后的分区需要输 ...
- 【Android-开发环境】 eclipse开发环境搭建
1.下载安装JDK jdk下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- 洛谷P2796 Facer的程序
洛谷题目链接 动态规划 我们看题目后知道这是一棵无根树,要求出有多少子树 我们设$f[u][1]$表示选了当前节点$u$的方案数 相反的$f[u][0]$则为不选中$u$ 那么考虑状态转移如下: f[ ...
- 【luoguP1991】 无线通讯网--最小生成树
题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...