[codility]Abs-distinct
来源:互联网 发布:淘宝客服服务态度培训 编辑:程序博客网 时间:2024/05/12 11:55
// you can also use includes, for example:// #include <algorithm>int solution(const vector<int> &A) { // write your code in C++98 //...traverse the array from both direction, and move according to the //absolute value int result = 0; long long prevAbsoluteValue = -1; for(int i = 0, j = A.size()-1; i <= j; ) { long long absValueLeft = A[i]; //we need use llabs to get the absolute value of long long instead of abs //to avoid overflow absValueLeft = llabs(absValueLeft); long long absValueRight = A[j]; absValueRight = llabs(absValueRight); long long absBiggerNumber = max(absValueLeft, absValueRight); if(absValueLeft > absValueRight) i++; else j--; if(prevAbsoluteValue != absBiggerNumber) result++; prevAbsoluteValue = absBiggerNumber; } //...return result return result;}