Java 计算数组中相同元素的唯一相邻对的数目

Java 计算数组中相同元素的唯一相邻对的数目,java,arrays,Java,Arrays,我的函数没有返回正确的答案。我的任务是计算数组中相同且相邻的两个数字这里是一个示例: array_1 = {1,2,2,3,4,4,2}; ans = 2 array_2 = {2,2,3,4,4}; ans = 2 array_3 = {1,1,1,1,1,1,1}; ans = 1 array_4 = {1,2,3,4,2}; ans = 0 这是我的功能。观察到的输出作为注释给出 public class countClampsTest { pub

我的函数没有返回正确的答案。我的任务是计算数组中相同且相邻的两个数字这里是一个示例:

array_1 = {1,2,2,3,4,4,2};   ans = 2
array_2 = {2,2,3,4,4};       ans = 2
array_3 = {1,1,1,1,1,1,1};   ans = 1
array_4 = {1,2,3,4,2};       ans = 0
这是我的功能。观察到的输出作为注释给出

public class countClampsTest
{
    public static void main(String[] args)
    {
        int array_1[] = {1,2,2,3,4,4,2}; // expected result: 2
        int array_2[] = {2,2,3,4,4};     // expected result: 2
        int array_3[] = {1,1,1,1,1,1,1}; // expected result: 1
        int array_4[] = {1,2,3,4,2};     // expected result: 0

        System.out.println(countClamps(array_1)); // Returns 2
        System.out.println(countClamps(array_2)); // Returns 1
        System.out.println(countClamps(array_3)); // Returns 1
        System.out.println(countClamps(array_4)); // Returns 0
    }

    static int countClamps(int[] arr) {
        int result = 0;
        int nextNext = 0;
        for (int current = 0; current < arr.length - 1; current++) {
            for (int next = current; next <= current + 1; next++) {
                nextNext = next  + 1;
                if(nextNext >= arr.length) {
                    nextNext = arr.length -1;
                }
                if (arr[current] == arr[next] && current != next) {
                    if(arr[next] != arr[nextNext]) {
                        result++;   
                    } else if(arr[next] == arr[nextNext] && arr.length % 2 == 0) {
                        result = 1;
                    }
                }
            }
        }
        return result;
    }
}
公共类countClampsTest
{
公共静态void main(字符串[]args)
{
int数组_1[]={1,2,2,3,4,4,2};//预期结果:2
int数组_2[]={2,2,3,4,4};//预期结果:2
int数组_3[]={1,1,1,1,1,1};//预期结果:1
int数组_4[]={1,2,3,4,2};//预期结果:0
System.out.println(countclips(数组_1));//返回2
System.out.println(countclips(数组_2));//返回1
System.out.println(countclips(数组_3));//返回1
System.out.println(countclips(数组_4));//返回0
}
静态整数计数夹具(整数[]arr){
int结果=0;
int-nextNext=0;
用于(int current=0;电流
一个可能的解决方案是:提前停止循环一次:使用
current
(这将导致
nextNext
始终位于数组中)。在外循环之后,只需比较数组的最后两个元素。如果它们相等,则将结果加1。完成了


除非数组的长度为0或1,否则需要对其进行特殊处理。

我添加了一个额外的布尔值,以跟踪已计数的重复项

static int countClamps(int[] arr) {
    int result = 0;
    int prev = 0;
    boolean same = false;
    for(int i = 0; i < arr.length; i++) {
        if (i == 0) {
            prev = arr[i];
        } else {
            if (arr[i] == prev) {
                if (!same) {
                    result++;
                    same = true;
                }
            } else {
                prev = arr[i];
                same = false;
            }
        }
    }
    return result;
}
static int countclips(int[]arr){
int结果=0;
int prev=0;
布尔相同=假;
对于(int i=0;i
我发现您的代码中存在一些问题

在测试中:

            if (arr[current] == arr[next] && current != next) {
你比较当前和下一个;通过在电流+1而不是电流下启动下一个回路,可以获得相同的结果:

        for (int next = current+1; next <= current + 1; next++) {
            nextNext = next  + 1;
            ...
            if (arr[current] == arr[next]) {
               ...
            }
        }
现在,在您的内部测试中:

                if(arr[next] != arr[nextNext]) {
                    result++;   
                } else if(arr[next] == arr[nextNext] && arr.length % 2 == 0) {
                    result = 1;
                }
您可以看到,第二个条件的第一部分始终为真,因此它等效于:

            int next = current+1;
            nextNext = next  + 1;
            ...
            if (arr[current] == arr[next]) {
               ...
            }
                if(arr[next] != arr[nextNext]) {
                    result++;   
                } else if(arr.length % 2 == 0) {
                    result = 1;
                }
剩下的条件意味着,如果数组的元素数为偶数,则将结果重置为1(为什么?)

在数组的末尾,如果最后两个元素相等,结果将不会递增,这就是为什么在第二种情况下不能得到正确的结果

以下是我的解决方案:

static int countClamps(int[] arr) {
    int result = 0;
    for (int i = 1; i < arr.length; ++i) {
        if (arr[i] == arr[i-1]
                && (i == arr.length-1 || arr[i] != arr[i+1])) {
            ++result;
        }
    }
    return result;
}
static int countclips(int[]arr){
int结果=0;
对于(int i=1;i
一旦找到一对,就跳过循环中的索引

    public static int pairs(int[] list){
      int pairs = 0;
      for(int x = 1; x < list.length; x++){
        if(list[x] == list[x-1]){
          pairs++;
          x=x+1;}
      }
      return pairs;
    }
公共静态int对(int[]列表){
整数对=0;
对于(int x=1;x
为什么数组1的答案是3?只有两对紧挨着的数字?您可能需要进一步解释一下……您的任务描述不清楚。为什么第一个答案应该是3?我看到两对数字“相同且彼此相邻”,即
2,2
4,4
,那么3的答案是什么呢?谢谢@BretC它应该是2编辑它以进行更正为什么这个逻辑需要嵌套循环?重新考虑你需要做什么,因为你应该只有一个循环。为什么你有一个循环(内部循环)从当前的到当前的?为什么
arr.length%2
会影响找到一对呢?代码不错。然而,我认为如果让他或她来编写代码,OP会得到更好的帮助。更有用的答案可能会发现原始代码中的错误/问题和/或勾勒出更好的算法。我会注意到这一点,但我会尝试根据您的代码创建类似的解决方案,谢谢!这是另一个伟大的解决方案!我将尝试将您的解决方案转换为代码谢谢!关于我的逻辑解决方案,你已经解决了我的问题!谢谢你的解释!一个问题是,为什么您的代码没有遇到arr[i+1]的索引越界?这是因为对
|
进行了快捷计算。当
i
位于最后一个索引时,
i==arr.length-1
为真,因此Java已经知道整个
i==arr.length-1 | | arr[i]!=arr[i+1]
将为true,在本例中,它不会查看表达式的第二部分,因此永远不会尝试
arr[i+1]