900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence.
The iterator is initialized by
RLEIterator(int[] A)
, whereA
is a run-length encoding of some sequence. More specifically, for all eveni
,A[i]
tells us the number of times that the non-negative integer valueA[i+1]
is repeated in the sequence.The iterator supports one function:
next(int n)
, which exhausts the nextn
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
.
Approach #1: Array. [Java]
- class RLEIterator {
- int index;
- int[] A;
- public RLEIterator(int[] A) {
- this.A = A;
- index = 0;
- }
- public int next(int n) {
- while (index < A.length && n > A[index]) {
- n = n - A[index];
- index += 2;
- }
- if (index >= A.length) return -1;
- A[index] = A[index] - n;
- return A[index+1];
- }
- }
- /**
- * Your RLEIterator object will be instantiated and called as such:
- * RLEIterator obj = new RLEIterator(A);
- * int param_1 = obj.next(n);
- */
Refereence:
https://leetcode.com/problems/rle-iterator/discuss/168294/Java-Straightforward-Solution-O(n)-time-O(1)-space
900. RLE Iterator的更多相关文章
- LC 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- [LeetCode] 900. RLE Iterator RLE迭代器
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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 菜刀连接webshell
中国菜刀,一个非常好用而又强大的webshell,它可不是用来切菜的做饭的道具哦,是一款专业的网站管理软件,大小只有300多KB,真是小巧实用啊!不过被不法分子利用到,就是一个黑站的利器了.我记得以前 ...
- SNP问题大集锦
SNP问题大集锦 [2017-01-19] 最近小编对基因检测很感兴趣,也跟风去测了一下,这一测不要紧,吓得小编几天没睡着觉,这不,检测报告上称小编的减肥能力弱,虽然小编一家都是胖子,唯有 ...
- 20172325 2017-2018-2 《Java程序设计》第六周学习总结
20172325 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 1.利用[ ]建立一个数组,整列数据可以通过数组名引用,数组中的每个元素则可以通过其在数组中 ...
- 80% UI 初学者走过的弯路,你走了几条?
关于UI 对于初学UI设计的人而言,可能对UI具体是做什么,或者自己是否能顺利转行胜任这样的岗位存在一定的顾虑,今天我们就来重点说说UI是做什么的,以及学UI到有哪些需要避免的弯路. 1.UI设计是做 ...
- mysq 日期l查询
pym=mysql(host = '#', port = 3306, user = '#',passworld='#',database='#') #根据起始和结束时间 charge_sql = 'S ...
- Django介绍(3)
https://www.cnblogs.com/yuanchenqi/articles/5786089.html
- Django入门与实践-第17章:保护视图(完结)
http://127.0.0.1:8000/boards/1/ #boards/views.py from django.contrib.auth.decorators import login_re ...
- systemctl自定义service
应用场景:开启自启动运行脚本/usr/local/manage.sh 一.服务介绍 CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户 ...
- java中线程和并发面试题
http://www.cnblogs.com/dolphin0520/p/3932934.html http://www.cnblogs.com/dolphin0520/p/3958019.html ...
- Python中的replace方法
replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...