Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绝对不同数 我正在通过C++编写一篇关于CODILITY的练习。问题是:_C++_Arrays_Algorithm - Fatal编程技术网

绝对不同数 我正在通过C++编写一篇关于CODILITY的练习。问题是:

绝对不同数 我正在通过C++编写一篇关于CODILITY的练习。问题是:,c++,arrays,algorithm,C++,Arrays,Algorithm,给出了由N个数组成的非空零索引数组A。这个 数组按非降序排序。绝对不同计数 此数组的值是数组中不同绝对值的数目 数组的元素 例如,考虑数组A这样: A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6 A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6 此数组的绝对不同计数为5,因为此数组的元素中有5个不同的绝对值, 即0、1、3、5和6 编写一个函数:

给出了由N个数组成的非空零索引数组A。这个 数组按非降序排序。绝对不同计数 此数组的值是数组中不同绝对值的数目 数组的元素

例如,考虑数组A这样:

A[0] = -5
A[1] = -3
A[2] = -1
A[3] =  0
A[4] =  3
A[5] =  6 
A[0] = -5
A[1] = -3
A[2] = -1
A[3] =  0
A[4] =  3
A[5] =  6 
此数组的绝对不同计数为5,因为此数组的元素中有5个不同的绝对值, 即0、1、3、5和6

编写一个函数:

int解决方案(向量&A)

给定一个由N个数字组成的非空零索引数组, 返回数组A的绝对不同计数

例如,给定一个数组,使得:

A[0] = -5
A[1] = -3
A[2] = -1
A[3] =  0
A[4] =  3
A[5] =  6 
A[0] = -5
A[1] = -3
A[2] = -1
A[3] =  0
A[4] =  3
A[5] =  6 
函数应该返回5,如上所述

假设:

N
是范围
[1..100000]
内的整数
数组的每个元素
A
是范围
[−2147483648..2147483647]

大堆
A
按非降序排序

复杂性:

预期最坏情况时间复杂度为
O(N)
期望最坏情况空间 复杂性是
O(N)
,超出了输入存储(不包括存储 输入参数是必需的)

输入数组的元素可以是 修改

我正在写下面的代码,我在代码中没有发现任何问题,但它就是没有通过

#include <algorithm>
#include <vector>
#include <cmath>


int solution(vector<int> &A) {
    int N(A.size());
    vector<long long> B(N,0);
    int counter(1);
    //int index(0);
    int move1(0);
    int move2(N-1);
    if(N==1)
        {return 1;}
    if(N==0)
        {return 0;}
    if(N==2)
        {
            if(abs(A[0])==abs(A[1]))
                {return 1;}
            else{return 2;}
        }

    for (int i = 0 ; i < N ; ++i)
        {
            B[i]=abs((long long )A[i]);
        }


    while(move1<move2)
        {
            if(B[move1]==B[move1+1])
                {move1+=1;}

            else if(B[move2]==B[move2]-1)
                {move2-=1;}

            else if(B[move1]>B[move2])
                {
                    counter+=1;
                    move1+=1;
                }
            else if(B[move1]<B[move2])
                {
                    counter+=1;
                    move2-=1;
                }
            else{move1+=1;}
        }

    return counter;
}
#包括
有一些错误,但我不能找出它的细节,如果有人可以帮助我的代码,我会非常感谢


谢谢

您可能需要一个迭代解决方案。从两端开始,朝着0向内移动

#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

size_t solution( const std::vector< int > & A )
{
    std::vector< int >::const_iterator f( A.begin() );
    std::vector< int >::const_reverse_iterator b( A.rbegin() );

    size_t result = 0;
    if( A.size() )
        for( ; ( f != A.end() ) && ( b != A.rend() ); )
        {
            if( *f >= 0 )
                return result + ( ( A.end() - f ) - ( b - A.rbegin() ) );
            else if( *b <= 0 )
                return result + ( ( A.rend() - b ) - ( f - A.begin() ) );
            else if( *f == -*b )
                ++result, ++f, ++b;
            else if( *f > -*b )
                ++result, ++b;
            else
                ++result, ++f;
        }
    return result;
}


int main( int, char ** )
{
    std::cout << solution( std::vector< int >{ -5, -3, -1, 0, 3, 6} ) << std::endl;
    std::cout << solution( std::vector< int >{ -5, -3, -1, 0, 1, 3, 6} ) << std::endl;
    std::cout << solution( std::vector< int >{ -5, -3, -1, 0, 2, 3, 6} ) << std::endl;
    std::cout << solution( std::vector< int >{ -5, -3, -1, 3, 6} ) << std::endl;
    std::cout << solution( std::vector< int >{ -5, -3, -1, 0, 3, 4, 5} ) << std::endl;
    return 0;
}
#包括
#包括
#包括
#包括
大小解决方案(常数std::vector&A)
{
std::vector::常量迭代器f(A.begin());
std::vector::const_reverse_迭代器b(A.rbegin());
大小\u t结果=0;
如果(A.size())
对于(;(f!=A.end())&&(b!=A.rend());)
{
如果(*f>=0)
返回结果+((A.end()-f)-(b-A.rbegin());
否则,如果(*b-*b)
++结果++b;
其他的
++结果+f;
}
返回结果;
}
int main(int,char**)
{

std::cout{5,-3,-1,0,3,6})100%红宝石溶液

def solution(a)
  a.each_with_object({}){ |el, acc| acc[el.abs] = true }.size
end


使用Java8流获得100/100

return (int) Arrays.stream(A).map(Math::abs)
            .distinct().count();

你被否决的原因有几个。1.你需要把问题归结为尽可能小的解释和例子,这有很多重复和不必要的地方。2.这听起来像是一个家庭作业问题(这里的人不喜欢这个问题,即使可以在家庭作业上得到帮助).3.您基本上是要求我们为您调试代码,这是一个没有特定代码问题的离题问题(而您的是一个一般性的“这不起作用”问题)@KevinWells Codibility是一个编程挑战网站,不是一个真正的家庭作业。如果你参加Codibility测试是为了被聘为程序员,那么你肯定应该知道如何使用调试器对代码进行故障排除。@Barmar即使在这里不是特别禁止的家庭作业,我只是给出了被否决的可能原因d因为我知道很多新用户在获得否决票和没有评论时感到沮丧。我没有否决,但我确实将其标记为非主题是的,我无法明确解释这个问题,因为我不知道Codibility如何处理它。我唯一知道的是,它返回12,而对于简单的非负数,结果应该是11。