首页 > 其他分享 >利用pair容器计数

利用pair容器计数

时间:2022-10-26 21:38:59浏览次数:76  
标签:容器 RLEIterator sequence int term next 计数 second pair


900. RLE Iterator

Medium

14158FavoriteShare

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.


class RLEIterator {
private:
vector<pair<int,int>> Vec;
public:
RLEIterator(vector<int>& A) {
for(int i = 0;i < A.size();i = i + 2){
if(A[i] == 0){continue;}
Vec.push_back(make_pair(A[i + 1],A[i]));
}
}

int next(int n) {
for(auto &a : Vec){
if(a.second == 0){
continue;
}
if(a.second >= n){
a.second = a.second - n;
return a.first;
}
n = n - a.second;
a.second = 0;
}
return -1;
}
};
/**
* Your RLEIterator object will be instantiated and called as such:
* RLEIterator* obj = new RLEIterator(A);
* int param_1 = obj->next(n);
*/

 

标签:容器,RLEIterator,sequence,int,term,next,计数,second,pair
From: https://blog.51cto.com/u_13121994/5798558

相关文章

  • docker笔记:docker容器通信参数 --link参数介绍
    1、link参数作用同一个宿主主机上的多个docker容器之间如果需要进行通信,第一种最容易想到的方式就是使用容器自身的ip地址、宿主主机的ip+容器暴露出的端口号来通信,我们知......
  • map的key是vector容器
    题目描述东东在一本古籍上看到有一种神奇数,如果能够将一个数的数字分成两组,其中一组数字的和等于另一组数字的和,我们就将这个数称为神奇数。例如242就是一个神奇数,我们......
  • bitset容器找出0~n-1中重复的那个数字
    题目描述一组无序的自然数集合,由0,1,2......,n的数字和一个的数字X(X>=0&&X<=n)组成,请从集合中找出这个重复数字X。输入描述:空格分割的自然数集合输出描述:重复数字......
  • 列表--list容器的使用(STL熟练掌握)
    题目描述一个学校里老师要将班上NN个同学排成一列,同学被编号为1\simN1∼N,他采取如下的方法:先将11号同学安排进队列,这时队列中只有他一个人;2-N2−N号同学依次入列,编号为i的......
  • Queue容器
      ......
  • 使用KeyPairGenerator生成公私钥对(oppo平台需要)
    下面是oppo平台的生成公钥的要求公钥需要使用RSA算法生成,1024位,生成后使用Base64进行编码,编码后的长度是216位Base64使用了Apache的commons-codec工具包,这个......
  • zk,kafka,redis哨兵,mysql容器化
    1.zookeeper,kafka容器化1.1zookeeper+kafka单机docker模式dockerpullbitnami/zookeeper:3.6.3-debian-11-r46dockerpullbitnami/kafka:3.1.1-debian-11-r36dock......
  • 图像处理:人群计数中密度图的生成——以ShanghaiTechA数据集为例
    1.前言记录密度图的生成,防止以后找不到。代码也是从别人那得来的2.高斯核模糊在生成伪装密度图时,使用如下公式:\[F(x)=\sum^N_{i=1}\delta(x-x_i)*G_{\sigma}(x)......
  • 【docker】拷贝容器里的文件至主机
    例如dockercpqinglong:/ql/data/root/testtest其中qinglong为容器名,/ql/data为要拷贝的文件夹,这里要写绝对路径/root/testtest为主机所在文件夹testtest回车等待即可......
  • 使用docker容器配置mysql的主从同步
    目录 mysql主从复制同步一前提条件二mysql集群配置文件1 创建配置集结构如下2创建节点配置文件(master.cnf与slave.cnf)三、搭建mysql环境创建mysql容器......