leetcode 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 to RLEIterator.next(int n) per test case.
Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9.
题意:每次取n个数,返回这n个数,最后的那一个。
模拟一下。
class RLEIterator {
public:
queue<pair<int,int> > q;
RLEIterator(vector<int> A) {
for (int i = 0; i < A.size()-1; i+= 2) {
int x = A[i];
int y = A[i+1];
//mp[y] = x;// y有x个
q.push({y,x});
}
}
int next(int n) {
while (!q.empty() && n > 0) {
auto &x = q.front();
if (x.second >= n) {
x.second -= n;
if (x.second == 0) q.pop();
return x.first;
} else {
n -= x.second;
q.pop();
}
}
return -1;
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator obj = new RLEIterator(A);
* int param_1 = obj.next(n);
*/
leetcode 900. RLE Iterator的更多相关文章
- [LeetCode] 900. RLE Iterator RLE迭代器
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- LC 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 ...
- 900. RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- [Swift]LeetCode900. RLE 迭代器 | RLE Iterator
Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
- [LeetCode] 284. Peeking Iterator 瞥一眼迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
随机推荐
- UICollectionView的header悬停
UICollectionView的header悬停,继承UICollectionViewFlowLayout,重写相关方法 // // StickyHeaderLayout.h // Wombat / ...
- Ffmpeg 探测输入源类型
本文主要记录ffmpeg探测输入源的几个点,方便以后查阅代码.虽然项目时间很赶,还是找个时间做个记录, 好记性不如不如备忘录,时间久了终是会忘记.Ffmpeg版本为4.0.2,版本很重要,Ffmpeg ...
- Eclipse配色利器
1 http://eclipsecolorthemes.org/ 这是官网 2 安装后,window-preferences-general-appearance-color theme 即可找到多 ...
- mysqldumpslow命令
mysqldumpslow --help显示如下 Parse and summarize the MySQL slow query log. Options are --verbose verbose ...
- 正则表达式 判断IP 数字
1.正则表达式 public static bool checkIP(string strIP) { //string regex = @"^(2[0-4]\d | 25[0-5] | [0 ...
- karma + phantom + mocha + sion + chai + nightwatch + selenium2(webdriver) 测试框架学习
第三方的教程传送门 https://segmentfault.com/a/1190000004558796 karma # github https://github.com/karma-runner ...
- mysql更改表结构:添加、删除、修改字段、调整字段顺序
添加字段: alter table `user_movement_log` Add column GatewayId int not null default 0 AFTER `Regionid` ( ...
- CCKiller:Linux轻量级CC攻击防御工具,秒级检查、自动拉黑和释放 《CCKiller:Linux轻量级CC攻击防御工具,秒级检查、自动拉黑和释放》来自张戈博客
张戈博客很久以前分享过一个CC攻击的防御脚本,写得不怎么样,不过被51CTO意外转载了.博客从此走上了经常被人拿来练手的不归之路. 当然,还是有不少朋友在生产环境使用,并且会留言询问相关问题.根据这些 ...
- 华为nova3超级慢动作酷玩抖音,没有办法我就是这么强大!
华为nova3超级慢动作酷玩抖音,没有办法我就是这么强大! 在华为最新发布的nova 3手机上,抖音通过华为himedia SDK集成了60fps.超级慢动作等华为媒体开放能力,在加持这些能力后,抖音 ...
- 常用PhpStorm 快捷键
函数列表 打开某一个源码文件后,保证鼠标焦点在源文件内,按键盘组合键: alt + 7 返回原文件导航:双击最上面的工程名即可 PhpStorm折叠文件内所有函数 按下快捷`Ctrl`+`Shift` ...