我试图解决this问题。统计数组中的不同切片
给出了一个整数M和一个非空的零索引数组A,其中包含N个非零的整数N 。在数组A的所有整数是小于 或等于M.
一对整数(P,Q)的,使得0≤P≤Q < N,被称为阵列A的切片 的切片由元素A [P],A [P + 1],..., A [Q]。不同的切片是仅由唯一数字组成的切片。 也就是说,切片中不会有多次出现单个号码。
例如,考虑整数M = 6和阵列A使得:
A[0] = 3 A[1] = 4 A[2] = 5 A[3] = 5 A[4] = 2
正好有九个不同的切片:(0,0),(0,1),(0,2), (1,0.1),(1,2),(2,2),(3,3),(3,4)和(4,4)。
目标是计算不同切片的数量。
在此先感谢。
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX 100002
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
using namespace std;
bool check[MAX];
int solution(int M, vector<int> &A) {
memset(check, false, sizeof(check));
int base = 0;
int fibot = 0;
int sum = 0;
while(fibot < A.size()){
if(check[A[fibot]]){
base = fibot;
}
check[A[fibot]] = true;
sum += fibot - base + 1;
fibot += 1;
}
return min(sum, 1000000000);
}
我明白了。非常感谢你。 –