// you can also use includes, for example:
// #include <algorithm>
int solution(vector<int> &A) {
// write your code in C++98
//...modify the input array, let all the elements in the right place
for(int i = 0; i < A.size(); ++i)
{
int curIdx = A[i]-1;
while(curIdx >= 0 && curIdx < A.size() && A[curIdx] != curIdx+1)
{
int nextIdx = A[curIdx]-1;
A[curIdx] = curIdx+1;
curIdx = nextIdx;
}
}
//...check if it's a permutation now
for(int i = 0; i < A.size(); ++i)
if(A[i] != i+1) return 0;
return 1;
}
[codility]Perm-Check
最新推荐文章于 2019-08-17 11:37:21 发布