Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/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
Arrays “我的代码”;只计算重复的次数”;它不起作用。根据我的说法,我已经写了清晰易懂的代码_Arrays_Sorting_Data Structures_Binary Search_Array Algorithms - Fatal编程技术网

Arrays “我的代码”;只计算重复的次数”;它不起作用。根据我的说法,我已经写了清晰易懂的代码

Arrays “我的代码”;只计算重复的次数”;它不起作用。根据我的说法,我已经写了清晰易懂的代码,arrays,sorting,data-structures,binary-search,array-algorithms,Arrays,Sorting,Data Structures,Binary Search,Array Algorithms,问题是: 这是我的密码: public static Point findRepeating(Integer arr[],int n) { // Point(return type) is a class having first and second as data memebres where first is number // repeating and second is number of times it is repeating if(arr[0]

问题是:

这是我的密码:

public static Point findRepeating(Integer arr[],int n)
{
    // Point(return type) is a class having first and second as data memebres where first is number  
    // repeating and second is number of times it is repeating

    if(arr[0] == arr[n-1])              // whole array has only one number 
        return new Point(arr[0],n);
        
    int low = 0,high = n-1,mid = (low+high)/2, repNumber = 0;
    
    while(high-low > 1)       // in this loop only I'm trying to find the number that is repeating
    {
        mid = (low+high)/2;
        
        if(arr[mid] == arr[mid-1] || arr[mid] == arr[mid+1])
        {
            repNumber = arr[mid];
            break;
        }
        
        if((arr[low]+(mid-low)) == arr[mid])
            low = mid+1;
        else
            high = mid;
    }
    int startIndex=0,endIndex=0;
    // now I'll find the start and end index of the repeating number
    // doing this by finding number just smaller and just larger than repeating number
    if(repNumber == arr[0])
    {
        startIndex = 0;
        endIndex = Arrays.binarySearch(arr,repNumber+1);
        return new Point(repNumber,endIndex);
    }
    if(repNumber == arr[n-1])
    {
        endIndex = n-1;
        startIndex = Arrays.binarySearch(arr,repNumber-1);
        return new Point(repNumber,endIndex-startIndex);
    }
    else
    {
        startIndex = Arrays.binarySearch(arr,repNumber-1) + 1;
        endIndex = Arrays.binarySearch(arr,repNumber+1);
        return new Point(repNumber,endIndex-startIndex);
    }
}  
我的代码失败的测试用例是(整个输入不可视,因为它很大):

回答错了!!!错误答案

对于多个测试用例(TC),您的代码可能无法正常工作

代码失败的第一个测试用例:

输入: 28566 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 124 125 126 127 128 129 130 131 132 133 134 135 136 137 139 140 141 142 143 144 145 146 147 148 149 150 15

其正确输出为: 1693210086

代码的输出是:
0 0

这个问题可以在小于O(N)(在最坏的情况下O(N))的时间复杂度内使用线性搜索简单地解决。 在第一步中,我们可以检查数组的第一个和最后一个元素是否相等

如果是, 我们可以返回数组的大小作为答案。 否则,我们可以使用两种方法进行线性检查

方法1:我们可以取两个数字之间的连续差,并检查结果是否为零

     int count = 0;
     vector<int>arr = { 1,2,2,2,2,2,2,3 }; 
for(int i = 0; i < arr.size() ; i++){
    if(arr[i+1] - arr[i] == 0 ){
        count++;
    }
}
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 }; 
 for(int i = 0; i < arr.size() ; i++){
    if((arr[i+1]^arr[i]) == 0 ){
        count++;
    }
}
int count=0;
向量arr={1,2,2,2,2,2,2,3};
对于(int i=0;i
方法2:对数组的两个连续元素进行异或运算,当异或为零时,递增计数

     int count = 0;
     vector<int>arr = { 1,2,2,2,2,2,2,3 }; 
for(int i = 0; i < arr.size() ; i++){
    if(arr[i+1] - arr[i] == 0 ){
        count++;
    }
}
int count = 0;
vector<int>arr = { 1,2,2,2,2,2,2,3 }; 
 for(int i = 0; i < arr.size() ; i++){
    if((arr[i+1]^arr[i]) == 0 ){
        count++;
    }
}
int count=0;
向量arr={1,2,2,2,2,2,2,3};
对于(int i=0;i
在我的机器上工作得非常好。也许你可以给我一些问题的提交链接,或者你的
Point
类的实现有一些问题。@risingStark非常感谢你的回复。实际上我已经买了Geeksforgeks DSA课程,所以你将无法访问提交链接。请看上面的链接。这是完全相似的。只是参数有向量,而实际问题有整数数组,它的大小作为输入。顺便说一句,链接是如果你可以访问。谢谢您所说的链接无法访问。您可以首先尝试运行普通的测试用例,看看这个特定测试用例或任何测试用例的代码是否失败。我想一定有一些自定义测试功能。在那里,您可以输入简单的测试用例和这个测试用例。我有你的代码失败的地方。@risingStark是的,有定制的测试输入可用,但坦率地说,我不知道“琐碎的测试用例”是什么意思。这就像是检查一些自定义输入和转角情况,我可以自己评估这些输入和转角情况,以便与代码的输出进行交叉检查?还有,您是如何提取代码失败的测试用例的?是任何特性还是您观察了预期的输出并生成了测试用例?我生成了测试用例,因为输入大小给定,所有元素都应该是连续的,并且重复元素的信息也给定了。对于琐碎的测试用例,您是对的,运行其中一些,看看您的代码是否按预期运行。