程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)?

开发过程中遇到我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)的问题如何解决?下面主要结合日常开发的经验,给出你关于我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)的解决方法建议,希望对你解决我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)有所启发或帮助;

问题是:https://www.geeksforgeeks.org/count-of-only-repeated-element-in-a-sorted-array-of-consecutive-elements/

这是我的代码:

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,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 3 4 3 4 3 4 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 5 3 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 15.................

它的正确输出是: 16932 10086

您的代码的输出是: 0 0

解决方法

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

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

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

     int count = 0;
     vector<int>arr = { 1,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,3 }; 
 for(int i = 0; i < arr.size() ; i++){
    if((arr[i+1]^arr[i]) == 0 ){
        count++;
    }
}

大佬总结

以上是大佬教程为你收集整理的我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)全部内容,希望文章能够帮你解决我的“仅重复计数”代码不起作用。编写了易于理解的干净代码(根据我的说法)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: