[codility]Prefix-set

来源:互联网 发布:windows引导命令 编辑:程序博客网 时间:2024/06/03 16:29
// you can also use includes, for example:// #include <algorithm>int solution(const vector<int> &A) {    // write your code in C++98    //...keep record of all the elements that exist in the array    vector<bool> existNum(A.size(), false);    int distinctNumCnt = 0;    for(int i = 0; i < A.size(); ++i)    {        if(!existNum[A[i]]) distinctNumCnt++;        existNum[A[i]] = true;    }    //...then, check if all the elements show up so far    int curDistinctNumCnt = 0;    for(int i = 0; i < A.size(); ++i)    {        if(existNum[A[i]])        {            existNum[A[i]] = false;            curDistinctNumCnt++;        }        if(curDistinctNumCnt == distinctNumCnt) return i;    }    }

原创粉丝点击