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 <= 1000A.lengthis an even integer.0 <= A[i] <= 10^9- There are at most
1000calls 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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- vscode go开发主要插件
• Colorization 代码着彩色 • Completion Lists 代码自动完成(使用gocode) • Snippets 代码片段 • Quick Info 快速提示信息(使用godef ...
- 远程操控批量复制应用(scp/pssh/pscp.pssh/rsync/pslurp)
scp命令: scp [options] SRC... DEST/两种方式: scp [options] [user@]host:/sourcefile /destpath scp [options] ...
- C# Winfrom TabControl美化
实例一: using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; na ...
- PAT Basic 1059 C语言竞赛 (20 分)
C 语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛.既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽: 0.冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……). 1.排名为素数的学生将 ...
- JavaScript 转换数字为整数的方法
本文将会列举并说明JavaScript 把一个number(或者numerical的对象)转换成一个整数相关方法. 使用parseInt parseInt的语法如下:parseInt(string, ...
- java——java跨平台原理
不同操作系统不同的虚拟机,屏蔽不同系统指令集的差异. 开发程序只需要遵循java规范:
- CSS 中用户自定义字体 @font-face
@font-face 允许网页中使用自定义的字体,这些自定义的字体被放置在服务器上,从而让网页摆脱对访问者计算机上字体环境的依赖. 简单的说,有了@font-face,只需将字体上传到服务器端,无论访 ...
- Java错误和异常解析
Java错误和异常解析 错误和异常 在Java中, 根据错误性质将运行错误分为两类: 错误和异常. 在Java程序的执行过程中, 如果出现了异常事件, 就会生成一个异常对象. 生成的异常对象将传递Ja ...
- Windows&Appium&Java&Python自动化测试-配置开发环境
摘要 本篇博文,主要介绍借助Appium做移动端自动化测试的开发环境搭建,包括Java和Python Java环境:Appium+Maven+Idea+TestNG+Reportng Python环境 ...
- JAI丢包掉帧处理
问题 时间戳停止变化/图像停止更新 描述 本小白刚刚接触JAI,有很多不懂的地方.这次遇到问题是请教了YZ大哥(不知道年龄,暂时这么称呼),很感谢YZ大哥的耐心指导.因为我不仅不知道怎么调,连在哪里调 ...